Files
AgentHub/server/routes/push.js
T
nntrivi2001 64017c6ca5 feat(run,notifications,profiles): stop sending prompts nobody typed, and fix notification + profile delivery
Run console:
- Picking a pipeline no longer pre-writes anything into the prompt box
  (`promptPrefill` in `LaneConsolePane.tsx` is gone — it wrote the driving
  slash command on a claude lane and a stage checklist on a codex one).
  Whatever sits in that box is sent as a real prompt the moment the run
  starts, so the box is the user's alone now.
- Resuming a session sends NO prompt at all (`handleStart` in `RunSetup.tsx`):
  the resumed session carries its own transcript, and leftover text in the box
  fired an extra turn into it.

Notifications:
- `client/src/lib/notify.ts` is now the ONE sender, used by both
  `useNotifications` and Settings' test button, and it reports where the
  notification landed: `POST /api/push/send` answering `{native:false,pushed:0}`
  means it went NOWHERE, so the client falls back to a local notification
  instead of counting the 200 as delivered.
- A notification carries the lane that raised it (`/workspace?lane=<id>`), and
  Workspace selects that lane on arrival.

Profiles:
- `server/lib/cc-profiles.js` reads and writes the macOS login Keychain item
  `Claude Code-credentials`, where the `claude` CLI actually keeps
  `claudeAiOauth` on darwin — a switch used to rewrite a file nothing reads.
  The file stays the fallback, is kept in step only when it already exists, and
  `AGENTHUB_CC_KEYCHAIN=0` pins it (tests set it).

Tests: npm run test:server (1820 pass), client vitest (553 pass).
2026-09-21 15:15:22 +07:00

54 lines
2.0 KiB
JavaScript

/**
* @file Express router for managing push notifications, providing endpoints to retrieve the VAPID public key, subscribe/unsubscribe to push notifications, and send push notifications to all subscribers. It interacts with the database to store subscription details and uses a push library to send notifications.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { Router } = require("express");
const { getPublicKey, sendPushToAll } = require("../lib/push");
const { db } = require("../db");
const router = Router();
router.get("/vapid-public-key", (_req, res) => {
res.json({ publicKey: getPublicKey() });
});
router.post("/subscribe", (req, res) => {
const { endpoint, keys } = req.body;
if (!endpoint || !keys?.p256dh || !keys?.auth) {
return res.status(400).json({ error: { message: "Missing required fields" } });
}
db.prepare(
"INSERT OR REPLACE INTO push_subscriptions (endpoint, p256dh, auth) VALUES (?, ?, ?)"
).run(endpoint, keys.p256dh, keys.auth);
res.json({ ok: true });
});
router.delete("/subscribe", (req, res) => {
const { endpoint } = req.body;
if (!endpoint) {
return res.status(400).json({ error: { message: "Missing endpoint" } });
}
db.prepare("DELETE FROM push_subscriptions WHERE endpoint = ?").run(endpoint);
res.json({ ok: true });
});
router.post("/send", async (req, res) => {
const { title, body, url } = req.body;
if (!title || !body) {
return res.status(400).json({ error: { message: "Missing title or body" } });
}
try {
// `result` tells the caller which surfaces actually fired:
// { native: true|false, pushed: <count>, failed: <count> }
// so a silent "no subscribers, no Electron host" no-op stops looking like
// success on the client side.
const result = await sendPushToAll(db, title, body, typeof url === "string" ? url : undefined);
res.json({ ok: true, ...result });
} catch (err) {
res.status(500).json({ error: { message: err.message } });
}
});
module.exports = router;