march-cli 0.1.2 → 0.1.4

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.
@@ -1,42 +1,101 @@
1
1
  import { spawn } from "node:child_process";
2
+ import nodeNotifier from "node-notifier";
3
+ import { fileURLToPath } from "node:url";
2
4
 
3
5
  const DEFAULT_BALLOON_TIMEOUT_MS = 5000;
6
+ const DEFAULT_NOTIFICATION_ICON_PATH = fileURLToPath(new URL("../assets/march-icon.png", import.meta.url));
4
7
 
5
8
  export function createDesktopTurnNotifier({
6
9
  enabled = true,
7
10
  platform = process.platform,
8
11
  spawnProcess = spawn,
12
+ writeBell = () => process.stdout.write("\x07"),
13
+ toastNotifier = nodeNotifier,
14
+ config = {},
9
15
  } = {}) {
16
+ const channels = resolveNotificationChannels(config);
17
+ const minDurationMs = normalizeNonNegativeInteger(config.minDurationMs, 0);
18
+ const toastSound = normalizeNotificationSound(config.sound, true);
10
19
  return {
11
20
  async notifyTurnEnd(event) {
12
- if (!enabled) return { ok: false, reason: "disabled" };
13
- return sendDesktopNotification({
14
- platform,
15
- spawnProcess,
16
- title: event?.title ?? defaultTurnTitle(event?.status),
17
- message: event?.message ?? defaultTurnMessage(event),
18
- });
21
+ const normalizedEvent = normalizeTurnEvent(event);
22
+ if (!enabled) return { ok: false, reason: "disabled", results: [] };
23
+ if (normalizedEvent.durationMs < minDurationMs) return { ok: false, reason: "min-duration", results: [] };
24
+
25
+ const payload = {
26
+ title: normalizedEvent.title ?? defaultTurnTitle(normalizedEvent.status),
27
+ message: normalizedEvent.message ?? defaultTurnMessage(normalizedEvent),
28
+ sound: toastSound,
29
+ };
30
+ const results = [];
31
+ if (channels.desktop) {
32
+ results.push({
33
+ channel: "desktop",
34
+ ...(await sendDesktopNotification({ platform, spawnProcess, toastNotifier, ...payload })),
35
+ });
36
+ }
37
+ if (channels.bell) results.push({ channel: "bell", ...sendBellNotification({ writeBell }) });
38
+ if (channels.command) {
39
+ results.push({
40
+ channel: "command",
41
+ ...sendCommandNotification({ spawnProcess, command: channels.command, event: normalizedEvent, ...payload }),
42
+ });
43
+ }
44
+
45
+ const delivered = results.some((result) => result.ok);
46
+ return {
47
+ ok: delivered,
48
+ reason: delivered ? undefined : results[0]?.reason ?? "no-channels",
49
+ results,
50
+ };
19
51
  },
20
52
  };
21
53
  }
22
54
 
23
- export async function sendDesktopNotification({ platform = process.platform, spawnProcess = spawn, title, message }) {
55
+ export async function sendDesktopNotification({ platform = process.platform, spawnProcess = spawn, toastNotifier = nodeNotifier, title, message, iconPath = DEFAULT_NOTIFICATION_ICON_PATH, sound = true }) {
24
56
  if (platform !== "win32") return { ok: false, reason: "unsupported-platform" };
25
57
 
26
58
  const safeTitle = normalizeNotificationText(title) || "March";
27
59
  const safeMessage = normalizeNotificationText(message) || "Turn finished";
28
- const script = buildWindowsBalloonScript({ title: safeTitle, message: safeMessage });
29
60
 
30
61
  try {
31
- const child = spawnProcess("powershell.exe", [
32
- "-NoProfile",
33
- "-ExecutionPolicy", "Bypass",
34
- "-WindowStyle", "Hidden",
35
- "-Command", script,
36
- ], {
62
+ const toastResult = await sendWindowsToastNotification({ toastNotifier, title: safeTitle, message: safeMessage, iconPath, sound });
63
+ if (toastResult.ok) return { ok: true };
64
+
65
+ const script = buildWindowsNotificationScript({ title: safeTitle, message: safeMessage, iconPath });
66
+ const balloonResult = await runWindowsNotificationPowerShell({ spawnProcess, script, timeoutMs: DEFAULT_BALLOON_TIMEOUT_MS + 5000 });
67
+ if (balloonResult.ok) return { ok: true, fallback: "balloon", toastReason: toastResult.reason };
68
+ return { ok: false, reason: `toast: ${toastResult.reason}; balloon: ${balloonResult.reason}` };
69
+ } catch (err) {
70
+ return { ok: false, reason: err?.message ?? String(err) };
71
+ }
72
+ }
73
+
74
+ export function sendBellNotification({ writeBell = () => process.stdout.write("\x07") } = {}) {
75
+ try {
76
+ writeBell("\x07");
77
+ return { ok: true };
78
+ } catch (err) {
79
+ return { ok: false, reason: err?.message ?? String(err) };
80
+ }
81
+ }
82
+
83
+ export function sendCommandNotification({ spawnProcess = spawn, command, event = {}, title, message }) {
84
+ if (!command) return { ok: false, reason: "command-not-configured" };
85
+ try {
86
+ const child = spawnProcess(String(command), [], {
87
+ shell: true,
37
88
  detached: true,
38
89
  stdio: "ignore",
39
90
  windowsHide: true,
91
+ env: {
92
+ ...process.env,
93
+ MARCH_NOTIFICATION_STATUS: String(event.status ?? ""),
94
+ MARCH_NOTIFICATION_TITLE: normalizeNotificationText(title),
95
+ MARCH_NOTIFICATION_MESSAGE: normalizeNotificationText(message),
96
+ MARCH_NOTIFICATION_SESSION: normalizeNotificationText(event.sessionName),
97
+ MARCH_NOTIFICATION_DURATION_MS: String(event.durationMs ?? 0),
98
+ },
40
99
  });
41
100
  child?.unref?.();
42
101
  return { ok: true };
@@ -45,31 +104,138 @@ export async function sendDesktopNotification({ platform = process.platform, spa
45
104
  }
46
105
  }
47
106
 
48
- export function buildWindowsBalloonScript({ title, message, timeoutMs = DEFAULT_BALLOON_TIMEOUT_MS }) {
107
+ export function buildWindowsBalloonScript({ title, message, timeoutMs = DEFAULT_BALLOON_TIMEOUT_MS, iconPath = DEFAULT_NOTIFICATION_ICON_PATH }) {
49
108
  const escapedTitle = escapePowerShellSingleQuotedString(title);
50
109
  const escapedMessage = escapePowerShellSingleQuotedString(message);
110
+ const escapedIconPath = escapePowerShellSingleQuotedString(iconPath);
51
111
  const timeout = Number.isFinite(timeoutMs) ? Math.max(0, Math.trunc(timeoutMs)) : DEFAULT_BALLOON_TIMEOUT_MS;
52
112
  return [
53
113
  "Add-Type -AssemblyName System.Windows.Forms",
54
114
  "Add-Type -AssemblyName System.Drawing",
55
115
  "$n = New-Object System.Windows.Forms.NotifyIcon",
56
- "$n.Icon = [System.Drawing.SystemIcons]::Information",
116
+ `$sourceBitmap = [System.Drawing.Bitmap]::FromFile('${escapedIconPath}')`,
117
+ "$trayBitmap = New-Object System.Drawing.Bitmap 32, 32",
118
+ "$graphics = [System.Drawing.Graphics]::FromImage($trayBitmap)",
119
+ "$graphics.Clear([System.Drawing.Color]::Transparent)",
120
+ "$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic",
121
+ "$graphics.DrawImage($sourceBitmap, 0, 0, 32, 32)",
122
+ "$icon = [System.Drawing.Icon]::FromHandle($trayBitmap.GetHicon())",
123
+ "$n.Icon = $icon",
124
+ "$n.Text = 'March'",
125
+ "$n.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::None",
57
126
  `$n.BalloonTipTitle = '${escapedTitle}'`,
58
127
  `$n.BalloonTipText = '${escapedMessage}'`,
59
128
  "$n.Visible = $true",
60
129
  `$n.ShowBalloonTip(${timeout})`,
61
130
  `Start-Sleep -Milliseconds ${timeout + 500}`,
62
131
  "$n.Dispose()",
132
+ "$icon.Dispose()",
133
+ "$graphics.Dispose()",
134
+ "$trayBitmap.Dispose()",
135
+ "$sourceBitmap.Dispose()",
63
136
  ].join("; ");
64
137
  }
65
138
 
66
- function defaultTurnTitle(status) {
67
- return status === "error" ? "March turn failed" : "March is ready";
139
+ export function buildWindowsNotificationScript({ title, message, timeoutMs = DEFAULT_BALLOON_TIMEOUT_MS, iconPath = DEFAULT_NOTIFICATION_ICON_PATH }) {
140
+ return buildWindowsBalloonScript({ title, message, timeoutMs, iconPath });
141
+ }
142
+
143
+ export function buildWindowsToastOptions({ title, message, iconPath = DEFAULT_NOTIFICATION_ICON_PATH, sound = true }) {
144
+ return {
145
+ title,
146
+ message,
147
+ icon: iconPath,
148
+ appID: "March",
149
+ sound,
150
+ wait: false,
151
+ };
152
+ }
153
+
154
+ function sendWindowsToastNotification({ toastNotifier = nodeNotifier, title, message, iconPath, sound }) {
155
+ return new Promise((resolve) => {
156
+ const notify = toastNotifier?.notify;
157
+ if (typeof notify !== "function") {
158
+ resolve({ ok: false, reason: "toast-notifier-unavailable" });
159
+ return;
160
+ }
161
+
162
+ let settled = false;
163
+ const timeout = setTimeout(() => finish({ ok: false, reason: "toast-timeout" }), DEFAULT_BALLOON_TIMEOUT_MS + 5000);
164
+ notify.call(toastNotifier, buildWindowsToastOptions({ title, message, iconPath, sound }), (err) => {
165
+ if (err) {
166
+ finish({ ok: false, reason: err?.message ?? String(err) });
167
+ return;
168
+ }
169
+ finish({ ok: true });
170
+ });
171
+
172
+ function finish(result) {
173
+ if (settled) return;
174
+ settled = true;
175
+ clearTimeout(timeout);
176
+ resolve(result);
177
+ }
178
+ });
179
+ }
180
+
181
+ function resolveNotificationChannels(config) {
182
+ return {
183
+ desktop: config.desktop !== false,
184
+ bell: config.bell === true,
185
+ command: typeof config.command === "string" && config.command.trim() ? config.command.trim() : null,
186
+ };
187
+ }
188
+
189
+ function runWindowsNotificationPowerShell({ spawnProcess, script, timeoutMs }) {
190
+ return new Promise((resolve) => {
191
+ const child = spawnProcess("powershell.exe", [
192
+ "-NoProfile",
193
+ "-ExecutionPolicy", "Bypass",
194
+ "-Command", script,
195
+ ], {
196
+ windowsHide: false,
197
+ stdio: ["ignore", "pipe", "pipe"],
198
+ });
199
+
200
+ let stderr = "";
201
+ let settled = false;
202
+ const timeout = setTimeout(() => finish({ ok: false, reason: "timeout" }), timeoutMs);
203
+
204
+ child?.stderr?.on?.("data", (chunk) => { stderr += chunk; });
205
+ child?.on?.("error", (err) => finish({ ok: false, reason: err?.message ?? String(err) }));
206
+ child?.on?.("close", (exitCode, signal) => {
207
+ if (exitCode === 0) {
208
+ finish({ ok: true });
209
+ return;
210
+ }
211
+ const detail = stderr.trim() || (signal ? `signal ${signal}` : `exit ${exitCode}`);
212
+ finish({ ok: false, reason: detail });
213
+ });
214
+
215
+ function finish(result) {
216
+ if (settled) return;
217
+ settled = true;
218
+ clearTimeout(timeout);
219
+ resolve(result);
220
+ }
221
+ });
222
+ }
223
+
224
+ function normalizeTurnEvent(event) {
225
+ return {
226
+ ...event,
227
+ status: event?.status === "error" ? "error" : "success",
228
+ durationMs: normalizeNonNegativeInteger(event?.durationMs, 0),
229
+ };
230
+ }
231
+
232
+ function defaultTurnTitle() {
233
+ return "March";
68
234
  }
69
235
 
70
236
  function defaultTurnMessage(event) {
71
237
  if (event?.status === "error") return event?.errorMessage ?? "Something went wrong";
72
- return event?.sessionName ? `${event.sessionName} is ready for review` : "Your turn is ready for review";
238
+ return event?.draft || "Turn finished";
73
239
  }
74
240
 
75
241
  function normalizeNotificationText(text) {
@@ -80,6 +246,17 @@ function normalizeNotificationText(text) {
80
246
  .slice(0, 240);
81
247
  }
82
248
 
249
+ function normalizeNotificationSound(value, fallback) {
250
+ if (value === false) return false;
251
+ if (typeof value === "string") return normalizeNotificationText(value) || fallback;
252
+ return value === undefined ? fallback : Boolean(value);
253
+ }
254
+
255
+ function normalizeNonNegativeInteger(value, fallback) {
256
+ const number = Number(value);
257
+ return Number.isFinite(number) ? Math.max(0, Math.trunc(number)) : fallback;
258
+ }
259
+
83
260
  function escapePowerShellSingleQuotedString(text) {
84
261
  return String(text).replaceAll("'", "''");
85
262
  }
@@ -22,6 +22,7 @@ export function captureContextSidecar(engine, metadata = {}) {
22
22
  sessionName: engine.sessionName ?? "",
23
23
  thinkingLevel: engine.thinkingLevel,
24
24
  namespace: engine.namespace,
25
+ pendingAssistantRecallHints: engine.pendingAssistantRecallHints ?? [],
25
26
  turns: engine.turns,
26
27
  };
27
28
  }