Skip to content

feat(assistant): detached chat jobs + subscribe stream + proactive messages#16

Merged
sbeashwar merged 1 commit into
mainfrom
feat/assistant-bidirectional-chat
May 3, 2026
Merged

feat(assistant): detached chat jobs + subscribe stream + proactive messages#16
sbeashwar merged 1 commit into
mainfrom
feat/assistant-bidirectional-chat

Conversation

@sbeashwar

Copy link
Copy Markdown
Owner

Summary

  • Long tasks no longer time out. POST /api/assistant/chat returns immediately with the message IDs; the LLM tool-loop runs as a detached background job and writes to the DB on completion. Proxy timeouts and phone-screen sleep stop killing in-flight turns.
  • New GET /api/assistant/stream is a long-lived SSE the client opens once per session via EventSource. Replays missed messages since a cursor on reconnect, then live-subscribes to bus events. 25s heartbeats keep proxy NATs warm. Auto-reconnect with exponential backoff is free from the browser.
  • New POST /api/assistant/fire (bearer-auth) lets background processes (reminders, SuNaPaNa email replies, future scanners) inject server-initiated assistant messages. They appear in the chat thread the moment they happen — chat now feels bidirectional.
  • In-process EventEmitter bus (apps/web/lib/assistant-bus.ts) connects detached jobs and the fire endpoint to open stream subscribers. Single-instance assumption; swap for Redis pub/sub if we ever scale out.
  • Schema: assistant_messages.status (pending/streaming/done/error) + startup repair to mark orphaned rows from a prior process as error.
  • Service worker push handler also postMessages open clients as a belt-and-suspenders refresh nudge.

Out-of-repo (in C:\mcp-servers)

  • reminders.py and sunnapana.py POST to /api/assistant/fire after firing push / sending an email reply, so the activity shows up in the chat thread too.
  • outlook-email/push_config.json gained assistant_fire_url.

Test plan

  • pnpm turbo run test --filter=@mysuperapp/web — 48/48 (5 new bus + 6 new fire route)
  • pnpm --filter @mysuperapp/web build clean, all /api/assistant/* routes registered
  • POST /api/assistant/fire smoke: lazily creates inbox conversation, persists message, returns IDs
  • Stream replay smoke: GET /api/assistant/stream?since=<iso> emits past messages
  • Live bus delivery smoke: open stream + concurrent fire → subscriber receives the event in real time
  • After merge: send a message that triggers multiple tool calls and lock the phone for 60+ seconds — confirm the answer arrives via stream replay rather than "trouble generating"
  • After merge: trigger SuNaPaNa scan or a reminder fire → confirm the activity shows up as a message in the inbox conversation in the PWA

🤖 Generated with Claude Code

…ssages

Replaces the single-request SSE chat (which timed out on long tool-loops
and couldn't deliver server-initiated content) with a job + subscribe
architecture:

- POST /api/assistant/chat now returns immediately with the message IDs.
  The LLM tool-loop runs as a detached background job that writes to the
  DB on completion. Long tasks no longer fail when the proxy times out
  or the phone screen sleeps.
- New GET /api/assistant/stream is a long-lived SSE the client opens
  once per session via EventSource. Replays missed messages since a
  cursor on reconnect, then live-subscribes to bus events. 25s
  heartbeats keep proxy NATs warm.
- New POST /api/assistant/fire (bearer-auth) lets background processes
  inject server-initiated assistant messages — they appear in the chat
  thread the moment they happen, not just as Web Push.
- In-process EventEmitter (apps/web/lib/assistant-bus.ts) connects the
  detached chat job and the fire endpoint to open stream subscribers.
- Schema: assistant_messages.status (pending/streaming/done/error) +
  startup repair to mark orphaned rows from a prior process as error.
- SW push handler also postMessage()s open clients as a belt-and-
  suspenders nudge to refresh.

Scanner integration (outside repo, in C:\mcp-servers):
- reminders.py and sunnapana.py post into the chat thread via the new
  endpoint after firing push / sending an email reply.

Tests: 12 new (5 bus + 6 fire route + existing). 48/48 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sbeashwar sbeashwar merged commit 22e9456 into main May 3, 2026
1 check passed
@sbeashwar sbeashwar deleted the feat/assistant-bidirectional-chat branch May 3, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant