From cb50e8c658201bcc072bbf96a394b1b7ab1c1ab9 Mon Sep 17 00:00:00 2001 From: Replit Agent Date: Thu, 19 Mar 2026 19:06:47 +0000 Subject: [PATCH] task-28 fix4: trivial cost-preview gate + job polling token 1. ui.js: _scheduleCostPreview() now gates on _TRIVIAL_RE before scheduling the /api/estimate fetch. Greeting-pattern text shows '0 sats' badge locally and never makes a network call. Same regex as edge-worker.js _isGreeting(). 2. payment.js: startPolling() GET /api/jobs/:id now attaches X-Nostr-Token header on every poll cycle via getOrRefreshToken(). Completes consistent X-Nostr-Token coverage across all job/session API calls. --- the-matrix/js/payment.js | 4 +++- the-matrix/js/ui.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/the-matrix/js/payment.js b/the-matrix/js/payment.js index 7492bd3..d1dfe19 100644 --- a/the-matrix/js/payment.js +++ b/the-matrix/js/payment.js @@ -163,7 +163,9 @@ function startPolling() { async function poll() { if (!currentJobId) return; try { - const res = await fetch(`${API_BASE}/jobs/${currentJobId}`); + const token = await getOrRefreshToken('/api'); + const pollHeaders = token ? { 'X-Nostr-Token': token } : {}; + const res = await fetch(`${API_BASE}/jobs/${currentJobId}`, { headers: pollHeaders }); const data = await res.json(); const { state, workInvoice, result, reason } = data; diff --git a/the-matrix/js/ui.js b/the-matrix/js/ui.js index dc5c700..4795eed 100644 --- a/the-matrix/js/ui.js +++ b/the-matrix/js/ui.js @@ -117,9 +117,18 @@ async function _fetchEstimate(text) { } } +// Fast trivial heuristic — same pattern as edge-worker.js _isGreeting(). +// Prevents /api/estimate network calls for greeting messages on every keypress. +const _TRIVIAL_RE = /^(hi|hey|hello|howdy|greetings|yo|sup|hiya|what'?s up)[!?.,]?\s*$/i; + function _scheduleCostPreview(text) { clearTimeout(_estimateTimer); if (!text || text.length < 4) { _hideCostPreview(); return; } + // Skip estimate entirely for trivially local messages — zero network calls + if (_TRIVIAL_RE.test(text.trim())) { + _showCostPreview('answered locally ⚡ 0 sats', '#44dd88'); + return; + } _estimateTimer = setTimeout(() => _fetchEstimate(text), 300); }