fix(desktop): drive SnoreToast directly with own named pipe + raw reply logging

node-notifier returned an empty result for text replies. Now spawn the bundled SnoreToast
with -tb -w -pipeName against our own pipe, read the raw UTF-16LE result, and parse the
reply (keeps spaces). Logs the raw pipe string to userData/toast-debug.log to pin the
exact reply field on real hardware.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 23:44:08 +05:30
parent 3f209bf619
commit 1abf6855d8
+40 -30
View File
@@ -35,13 +35,20 @@ ipcMain.on('get-install-info', (e) => {
// on the next restart. No-op in dev (unpackaged).
let autoUpdater = null;
try { ({ autoUpdater } = require('electron-updater')); } catch (_) { /* not installed in dev */ }
// Native Windows toast WITH a reply box, via node-notifier's bundled SnoreToast engine.
// node-notifier's API doesn't expose the reply text box, but its WindowsToaster forwards raw
// options to SnoreToast — so we inject `-tb` (text box) + `-p` (avatar image) and reuse its
// named-pipe + result parsing (exit 5 = TextEntered). Installed-app only (needs the
// AppUserModelID shortcut the NSIS installer registers). No PowerShell 7 required.
let WindowsToaster = null;
try { WindowsToaster = require('node-notifier').WindowsToaster; } catch (_) { /* optional */ }
// Native Windows toast WITH a reply box, driven by SnoreToast directly (the binary node-notifier
// bundles). We run it against our OWN named pipe so we fully control reading the typed reply.
// Installed-app only (needs the AppUserModelID shortcut the NSIS installer registers). No pwsh.
const { spawn } = require('child_process');
const net = require('net');
function snoreToastPath() {
try {
const dir = path.dirname(require.resolve('node-notifier')); // .../node-notifier
const exe = os.arch() === 'ia32' ? 'snoretoast-x86.exe' : 'snoretoast-x64.exe';
const p = path.join(dir, 'vendor', 'snoreToast', exe);
return p.replace('app.asar' + path.sep, 'app.asar.unpacked' + path.sep); // execute from the unpacked copy
} catch (_) { return null; }
}
// Write the avatar the renderer drew (a data: URL) to a temp PNG for SnoreToast's -p image.
function writeTempPng(dataUrl) {
@@ -55,31 +62,34 @@ function writeTempPng(dataUrl) {
// Resolves with {text} (replied), {open} (toast clicked), or null (dismissed/timeout).
ipcMain.handle('reply-notification', (_e, payload = {}) => new Promise((resolve) => {
if (!WindowsToaster) return resolve(null);
const exe = snoreToastPath();
if (!exe || !fs.existsSync(exe)) return resolve(null);
const img = writeTempPng(payload.avatar);
let done = false;
const finish = (v) => { if (!done) { done = true; if (img) { try { fs.unlinkSync(img); } catch (_) {} } resolve(v); } };
const pipeName = '\\\\.\\pipe\\bizc-toast-' + crypto.randomBytes(6).toString('hex');
let raw = Buffer.alloc(0), done = false, server = null;
const finish = (v) => { if (!done) { done = true; try { server && server.close(); } catch (_) {} if (img) { try { fs.unlinkSync(img); } catch (_) {} } resolve(v); } };
const parseAndFinish = (code) => {
const s = raw.toString('utf16le').replace(/[\u0000\r\n]+/g, '').trim();
try { fs.appendFileSync(path.join(app.getPath('userData'), 'toast-debug.log'), JSON.stringify({ t: Date.now(), code, raw: s }) + '\n'); } catch (_) {}
const r = {};
s.split(';').forEach((kv) => { const i = kv.indexOf('='); if (i > 0) r[kv.slice(0, i).trim().toLowerCase()] = kv.slice(i + 1); });
let text = String(r.text || r.value || r.userinput || r.textbox || r.reply || '').trim();
// SnoreToast exit 5 = TextEntered; if the reply isn't in a key=value field, take the raw value.
if (!text && code === 5 && s) { const parts = s.split('='); text = (parts.length > 1 ? parts.slice(1).join('=') : s).trim(); }
if (text) return finish({ kind: payload.kind, id: payload.id, text });
const act = String(r.action || '').toLowerCase();
if (code === 0 || act.includes('activat') || act.includes('click')) return finish({ kind: payload.kind, id: payload.id, open: true });
finish(null);
};
try {
const toaster = new WindowsToaster({ withFallback: false });
toaster.notify({
title: payload.title || 'Biz Connect',
message: payload.body || ' ',
appID: 'com.bizgaze.connect.desktop',
tb: true, // -tb : reply text box
w: true, // -w : wait for the user to reply / click before returning
...(img ? { icon: img } : {}), // mapped to -p (image)
}, (err, response, metadata) => {
// Log the raw result once so the exact reply field can be confirmed on a real machine.
try { fs.appendFileSync(path.join(app.getPath('userData'), 'toast-debug.log'), JSON.stringify({ t: Date.now(), err: err && err.message, response, metadata }) + '\n'); } catch (_) {}
const meta = metadata || {};
const known = ['click', 'activate', 'activated', 'timeout', 'timedout', 'dismissed'];
const respStr = String(response || '').trim();
let text = String(meta.text || meta.value || meta.reply || '').trim();
if (!text && respStr && !known.includes(respStr.toLowerCase())) text = respStr; // some builds return the reply as the response
const act = String(response || meta.action || meta.activationType || '').toLowerCase();
if (text) finish({ kind: payload.kind, id: payload.id, text });
else if (act.includes('activat') || act === 'click') finish({ kind: payload.kind, id: payload.id, open: true });
else finish(null);
server = net.createServer((sock) => { sock.on('data', (d) => { raw = Buffer.concat([raw, d]); }); });
server.on('error', () => finish(null));
server.listen(pipeName, () => {
const args = ['-t', payload.title || 'Biz Connect', '-m', payload.body || ' ', '-appID', 'com.bizgaze.connect.desktop', '-pipeName', pipeName, '-application', process.execPath, '-tb', '-w'];
if (img) args.push('-p', img);
const child = spawn(exe, args, { windowsHide: true });
child.on('error', () => finish(null));
child.on('exit', (code) => setTimeout(() => parseAndFinish(code == null ? -1 : code), 250));
});
} catch (_) { finish(null); }
}));