@wahooks/channel 2.1.4 → 2.2.1

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.
Files changed (2) hide show
  1. package/dist/index.js +55 -27
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -729,40 +729,68 @@ async function main() {
729
729
  claudeConnected = true;
730
730
  // Connect to real-time event stream
731
731
  connectWebSocket();
732
- // Process any pending reminders immediately
733
- await processPendingQueue();
734
- // Watch pending.json for changes (daemon writes to it)
732
+ // Process pending reminders via polling
735
733
  fs.mkdirSync(WAHOOKS_DIR, { recursive: true });
736
- // Ensure file exists for watching
737
734
  if (!fs.existsSync(PENDING_FILE)) {
738
735
  fs.writeFileSync(PENDING_FILE, "[]", { mode: 0o600 });
739
736
  }
740
- // Use both fs.watch and polling for robustness
741
- try {
742
- fs.watch(PENDING_FILE, () => {
743
- setTimeout(() => processPendingQueue(), 500); // debounce
744
- });
745
- }
746
- catch {
747
- // fs.watch not supported — fall back to polling only
748
- }
749
- // Poll every 10s as fallback
750
- setInterval(() => processPendingQueue(), 10_000);
751
- console.error("[wahooks-channel] Ready — WhatsApp messages will appear in Claude Code");
752
- // DEBUG: test delayed notification from within the channel
753
- setTimeout(async () => {
754
- console.error("[wahooks-channel] DEBUG: sending test notification from setTimeout");
737
+ async function pollPending() {
738
+ if (!claudeConnected)
739
+ return;
740
+ let pending;
755
741
  try {
756
- await mcp.notification({
757
- method: "notifications/claude/channel",
758
- params: { content: "DEBUG: Test notification from wahooks-channel setTimeout", meta: { from: "debug" } },
759
- });
760
- console.error("[wahooks-channel] DEBUG: notification sent OK");
742
+ const raw = fs.readFileSync(PENDING_FILE, "utf-8");
743
+ pending = JSON.parse(raw);
761
744
  }
762
- catch (e) {
763
- console.error("[wahooks-channel] DEBUG: notification FAILED:", e);
745
+ catch {
746
+ return;
747
+ }
748
+ if (!pending || pending.length === 0)
749
+ return;
750
+ // Prune stale (>24h)
751
+ const cutoff = new Date(Date.now() - 24 * 60 * 60 * 1000);
752
+ const active = pending.filter((p) => new Date(p.addedAt) > cutoff);
753
+ if (active.length === 0) {
754
+ fs.writeFileSync(PENDING_FILE, "[]", { mode: 0o600 });
755
+ return;
756
+ }
757
+ const debugLog = path.join(WAHOOKS_DIR, "channel-debug.log");
758
+ const log = (msg) => fs.appendFileSync(debugLog, `${new Date().toISOString()} ${msg}\n`);
759
+ log(`Found ${active.length} pending, claudeConnected=${claudeConnected}`);
760
+ console.error(`[wahooks-channel] Found ${active.length} pending reminder(s), delivering...`);
761
+ for (const item of active) {
762
+ log(`Delivering: ${item.reminderId}`);
763
+ console.error(`[wahooks-channel] Delivering: ${item.reminderId}`);
764
+ try {
765
+ const result = await mcp.notification({
766
+ method: "notifications/claude/channel",
767
+ params: {
768
+ content: `[Scheduled Reminder] ${item.task}\n\nTarget chat: ${item.chatId}\nScheduled for: ${item.scheduledFor}\n\nPlease execute this task now and send the results to the target chat using wahooks_reply.`,
769
+ meta: {
770
+ from: item.chatId,
771
+ reminder_id: item.reminderId,
772
+ },
773
+ },
774
+ });
775
+ log(`Delivered: ${item.reminderId}, result=${JSON.stringify(result)}`);
776
+ console.error(`[wahooks-channel] Delivered: ${item.reminderId}`);
777
+ }
778
+ catch (err) {
779
+ log(`FAILED: ${item.reminderId}, error=${err}`);
780
+ console.error(`[wahooks-channel] Delivery failed: ${err}`);
781
+ claudeConnected = false;
782
+ return;
783
+ }
764
784
  }
765
- }, 10000);
785
+ // Clear queue after all delivered
786
+ fs.writeFileSync(PENDING_FILE, "[]", { mode: 0o600 });
787
+ log("Queue cleared");
788
+ }
789
+ // Poll every 10s
790
+ setInterval(pollPending, 10_000);
791
+ // Also run once after 3s
792
+ setTimeout(pollPending, 3000);
793
+ console.error("[wahooks-channel] Ready — WhatsApp messages will appear in Claude Code");
766
794
  }
767
795
  main().catch((err) => {
768
796
  console.error("[wahooks-channel] Fatal:", err.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wahooks/channel",
3
- "version": "2.1.4",
3
+ "version": "2.2.1",
4
4
  "description": "WhatsApp channel for Claude Code — chat with Claude via WhatsApp",
5
5
  "type": "module",
6
6
  "bin": {