Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

PWA haptics: reply buzz once per message, 3 s throttle, own/backfill skip

Pixel (kwant bot)37334486f891
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 72378827..dd5984e1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- **Vibration on Android phones.** The web app gives a short buzz when you send a message, answer a
question or approval card, press **Solve it**, **Done**, **Skip** or **Hand back**, and when a puzzle
is solved, with a different buzz for errors. A bot reply arriving while the app is on screen gives a
- light double buzz, at most one per burst. A **Vibration** switch in App settings turns it off; it
+ light double buzz once per finished reply while the app is on screen, at most one every 3 seconds. A **Vibration** switch in App settings turns it off; it
starts off when the phone asks for reduced motion. iPhones and desktops have no web vibration.
### Changed
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index eb0575c9..4f403b7d 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -1026,7 +1026,8 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
const ctx = streamTarget(event, streamingContextIdRef.current);
if (ctx === openCtxRef.current) {
playSfx('coin');
- messageHaptic();
+ // Live events only (the stream has no replay; history loads via refreshContext).
+ messageHaptic({ id: typeof event.turn_id === 'string' ? event.turn_id : undefined });
setTokensUsed((prev) => prev + (event.data?.tokens || 0));
}
if (typeof event.turn_id === 'string') completed.add(event.turn_id);
diff --git a/frontend/src/haptics.ts b/frontend/src/haptics.ts
index 83bfa545..2df90085 100644
--- a/frontend/src/haptics.ts
+++ b/frontend/src/haptics.ts
@@ -15,8 +15,8 @@ export const HAPTIC_PATTERNS: Record<HapticKind, number | number[]> = {
export const HAPTICS_KEY = 'kwnt_haptics';
-/** Several replies landing together give one buzz. */
-export const MESSAGE_GAP_MS = 1500;
+/** Several replies landing together (one bot or many) give one buzz. */
+export const MESSAGE_GAP_MS = 3000;
/** What the helpers read; the browser's globals by default, fakes in tests. */
export interface HapticsEnv {
@@ -89,18 +89,43 @@ export function haptic(kind: HapticKind, env: HapticsEnv = browserEnv()): boolea
}
let lastMessageAt = -Infinity;
+const buzzedIds = new Set<string>();
-/** A bot's reply arrived: buzz only while the app is on screen, at most once per MESSAGE_GAP_MS. */
-export function messageHaptic(env: HapticsEnv = browserEnv(), now: number = Date.now()): boolean {
+/** What arrived. A finished bot reply is the only thing that buzzes. */
+export interface ArrivedMessage {
+ /** Turn or message id: the same reply never buzzes twice. */
+ id?: string;
+ /** The person's own message coming back: no buzz. */
+ fromPerson?: boolean;
+ /** History loaded on open or after a reconnect, not a new arrival: no buzz. */
+ backfill?: boolean;
+}
+
+/**
+ * A bot's reply finished arriving: buzz once, only while the app is on screen, at most once per
+ * MESSAGE_GAP_MS. Call it on the settled reply, not on streamed chunks.
+ */
+export function messageHaptic(
+ msg: ArrivedMessage = {},
+ env: HapticsEnv = browserEnv(),
+ now: number = Date.now(),
+): boolean {
+ if (msg.fromPerson || msg.backfill) return false;
+ if (msg.id && buzzedIds.has(msg.id)) return false;
// In the background the push notification already vibrates.
if (env.document && env.document.visibilityState !== 'visible') return false;
if (now - lastMessageAt < MESSAGE_GAP_MS) return false;
if (!haptic('message', env)) return false;
lastMessageAt = now;
+ if (msg.id) {
+ buzzedIds.add(msg.id);
+ if (buzzedIds.size > 200) buzzedIds.delete(buzzedIds.values().next().value as string);
+ }
return true;
}
/** Tests only. */
export function resetMessageHaptic(): void {
lastMessageAt = -Infinity;
+ buzzedIds.clear();
}
diff --git a/frontend/tests/haptics.test.mjs b/frontend/tests/haptics.test.mjs
index 1e7527a9..d5a4b6e6 100644
--- a/frontend/tests/haptics.test.mjs
+++ b/frontend/tests/haptics.test.mjs
@@ -103,17 +103,47 @@ test('no vibrate call before the first tap on the page', () => {
assert.deepEqual(p.calls, []);
});
-test('reply buzz: only on screen, one per burst', () => {
+test('reply buzz: hidden tab skips (the push notification vibrates instead)', () => {
h.resetMessageHaptic();
const hidden = phone({ visible: false });
- assert.equal(h.messageHaptic(hidden.env, 1000), false, 'background: the push notification vibrates instead');
+ assert.equal(h.messageHaptic({ id: 't1' }, hidden.env, 1000), false);
+ assert.deepEqual(hidden.calls, []);
+});
+
+test('reply buzz: once per message', () => {
+ h.resetMessageHaptic();
const p = phone();
- assert.equal(h.messageHaptic(p.env, 1000), true);
- assert.equal(h.messageHaptic(p.env, 1000 + h.MESSAGE_GAP_MS - 1), false, 'several bots at once give one buzz');
- assert.equal(h.messageHaptic(p.env, 1000 + h.MESSAGE_GAP_MS), true);
+ assert.equal(h.messageHaptic({ id: 't1' }, p.env, 1000), true);
+ assert.equal(h.messageHaptic({ id: 't1' }, p.env, 1000 + 10 * h.MESSAGE_GAP_MS), false, 'same reply again');
+ assert.equal(h.messageHaptic({ id: 't2' }, p.env, 1000 + 10 * h.MESSAGE_GAP_MS), true);
assert.deepEqual(p.calls, [h.HAPTIC_PATTERNS.message, h.HAPTIC_PATTERNS.message]);
+});
+
+test('reply buzz: own messages and history backfill skip', () => {
+ h.resetMessageHaptic();
+ const p = phone();
+ assert.equal(h.messageHaptic({ id: 'mine', fromPerson: true }, p.env, 1000), false);
+ assert.equal(h.messageHaptic({ id: 'old', backfill: true }, p.env, 2000), false);
+ assert.deepEqual(p.calls, []);
+});
+
+test('reply buzz: throttled, a burst from several bots gives one buzz', () => {
+ h.resetMessageHaptic();
+ assert.ok(h.MESSAGE_GAP_MS >= 3000);
+ const p = phone();
+ assert.equal(h.messageHaptic({ id: 'a' }, p.env, 1000), true);
+ assert.equal(h.messageHaptic({ id: 'b' }, p.env, 1200), false);
+ assert.equal(h.messageHaptic({ id: 'c' }, p.env, 1000 + h.MESSAGE_GAP_MS - 1), false);
+ assert.equal(h.messageHaptic({ id: 'd' }, p.env, 1000 + h.MESSAGE_GAP_MS), true);
+ assert.equal(p.calls.length, 2);
+});
+
+test('reply buzz: Vibration switch off silences it', () => {
+ h.resetMessageHaptic();
+ const p = phone();
h.setHapticsEnabled(false, p.env);
- assert.equal(h.messageHaptic(p.env, 99999), false);
+ assert.equal(h.messageHaptic({ id: 't1' }, p.env, 1000), false);
+ assert.deepEqual(p.calls, []);
});
test('wiring: meaningful actions buzz, typing and scrolling do not', () => {
@@ -131,7 +161,15 @@ test('wiring: meaningful actions buzz, typing and scrolling do not', () => {
assert.match(body('answerHumanStep'), /if \(which === 'done'\) haptic\('success'\)/);
assert.match(app, /if \(solve\) \{\s*setSolveRequest\(\(v\) => v \+ 1\);\s*haptic\('tap'\);/, 'Solve it taps');
// Replies: on TurnComplete for the open chat and when a discussion ends.
- assert.equal(app.match(/messageHaptic\(\)/g)?.length, 2);
+ // Settled replies only: never on streamed chunks, and the turn id dedupes repeats.
+ assert.equal(app.match(/messageHaptic\(/g)?.length, 2);
+ assert.match(app, /messageHaptic\(\{ id: typeof event\.turn_id === 'string' \? event\.turn_id : undefined \}\)/);
+ for (const chunk of ['TextDelta', 'ThinkingDelta']) {
+ const at = app.indexOf(`case '${chunk}'`);
+ if (at >= 0) assert.doesNotMatch(app.slice(at, app.indexOf('break;', at)), /messageHaptic/, chunk);
+ }
+ // History arrives through refreshContext/hydrateFromServer, which never buzz.
+ assert.doesNotMatch(app.slice(app.indexOf('sse.onopen'), app.indexOf('sse.onmessage')), /messageHaptic/);
assert.doesNotMatch(app, /onScroll[^\n]*haptic|onChange[^\n]*haptic|onInput[^\n]*haptic/);
const overlay = src('components/BrowserControlOverlay.tsx');