Skip to content

refactor(bot): one message pipeline for all chat platforms (code-shape 2/5) - #19

Merged
emanuilo merged 3 commits into
mainfrom
refactor/2-chat-pipeline
Nov 22, 2026
Merged

emanuilo merged 3 commits into
mainfrom
refactor/2-chat-pipeline

Conversation

@emanuilo

Copy link
Copy Markdown
Owner

Summary

Stack 2/5 of the code-shape restructuring (base: #18). Telegram, Slack and WhatsApp share one message pipeline; OpenAI is reached through one adapter.

 memclaw/bot/
+  __init__.py    PLATFORMS: name, label, run   (cli and the setup wizard read it)
+  inbound.py     voice -> text, link summaries, prompts, agent call, reminder delivery (each prompt text once)
+  commands.py    /model and /effort reply texts
-  handlers.py         -> telegram.py   (TelegramBot: receive, download, send, menus)
-  slack_handlers.py   -> slack.py      (SlackBot)
-  whatsapp_handlers.py-> whatsapp.py
-  link_processor.py   -> links.py
+memclaw/openai_client.py   the only `openai` importer: embeddings, Whisper, link summaries, doctor probe
-memclaw/openai_health.py   (merged into openai_client.py; report printing moved to setup.py)
 memclaw/cli.py
-  _run_telegram / _run_slack / _run_whatsapp   (near-copies)
+  PLATFORMS[name].run(config)
platform message
  telegram.py | slack.py | whatsapp.py      receive, download, access policy
    inbound.handle(...)                     transcribe, summarize links, build prompt
      agent.handle(...)
    send reply / images                     platform formatting

Adding a platform: one file + one PLATFORMS line (+ its settings in config.py, + its module name in three import contracts).

New guards: openai protected to memclaw.openai_client (import-linter + kit banned API); chat-platform contracts updated for the renamed modules; setup now sits above bot (the wizard reads PLATFORMS).

Evidence

  • Parity test tests/bot/test_parity.py (47 cases): the same scenarios through all three platforms — text, link, unfetchable page, photo with/without caption, voice ok/failed, stranger, /new, reminder, long reply, found images, plus each platform's quirks. Asserts the exact prompt the agent gets, the exact Whisper/summary calls, and what the user sees.
    • Before: added in commit 1 against the old code → passes (full old suite 498 passed).
    • After: same assertions pass on the new code; deliberately breaking a prompt string, the Slack allowlist rule or a newline in the link-summary text makes it fail.
  • memclaw old vs new with a temp HOME: --help, missing-token errors for every platform, status, terminal fallback print identically.
  • code-shape: duplicate-code entries 2 (31 lines) → 0; openai importers 7 → 1; baseline 63 → 39; cli.py 424 → 320 lines; Slack/WhatsApp files 358/321 → 279/246.
  • pytest: 501 passed, 20 skipped (was 451). ruff, lint-imports, code-shape check clean.

Merge Danger

Door: two-way

Blast Radius: chat platforms

  • Behaviour is meant to be identical per platform; the parity test pins it. Existing platform differences (empty Slack allowlist lets everyone in, reply limits, failed-download replies, WhatsApp shutdown, ...) are preserved on purpose and listed for a follow-up decision.
  • Small tidy-ups: two uncalled methods removed; Telegram reads the chat id from the message (same value, clears 30 mypy errors); a Telegram update without user/message is ignored instead of crashing.
  • Library/module paths change (memclaw.bot.handlers → memclaw.bot.telegram, MessageHandlers → TelegramBot, SlackHandlers → SlackBot, memclaw.openai_health removed).
  • mypy override: neonize.* has no type hints (ignore_missing_imports).
  • Stacked on refactor(config): declare each setting once (code-shape 1/5) #18; merge bottom-up.

🤖 Generated with Claude Code

Drives each platform at its SDK boundary with fakes (agent, OpenAI, web
page, chat SDKs) and pins what the agent receives and what the user sees:
plain text, a link, a photo with a captioned link, a failed and a working
voice note, a stranger, /new, a reminder, long replies, found images, plus
the per-platform quirks (empty allowlists, self-chat only, download errors).

Written against the current layout so the coming refactor of the three
handlers into one pipeline can prove it changes nothing: only the
"where the code lives" block at the top may change.
…ient.py

- OpenAIClient offers the three calls Memclaw makes: embed() (index.py),
  transcribe() (the three chat handlers' Whisper call) and chat() (link
  summaries). Same models, parameters and errors as before.
- The `memclaw doctor` probe (was openai_health.py) lives there too and uses
  those calls; printing its report moved to setup.py, next to the wizard
  that also shows it.
- New guard: an import-linter contract (and the kit's banned_api) lets only
  memclaw.openai_client import `openai`. It sits in its own layer, below
  index and above config.

The parity test passes unchanged except for how it hands the bots their
OpenAI client.
Each chat app re-implemented the same steps: voice to text, link summaries,
the prompts, the agent call, reminder delivery. Now:

- bot/inbound.py does those once, for every platform. The prompt texts and
  the transcription-failed reply are written there once (they were copied
  9 and 3 times).
- bot/telegram.py, slack.py and whatsapp.py (were handlers.py,
  slack_handlers.py, whatsapp_handlers.py) only do their app's I/O:
  receive, download, send, plus Telegram's /model and /effort menus. Each
  has a run(config) with what cli.py's _run_<platform> launchers did.
- bot/__init__.py has the PLATFORMS registry. `memclaw` starts a platform
  through it and the setup wizard lists the platforms from it, so a new
  platform is one module plus one registry line. A platform's module (and
  its SDK) is still only imported when that platform starts.
- bot/commands.py: the /model and /effort replies (Telegram adds buttons).
- bot/links.py was link_processor.py.

Guards: only memclaw.bot imports the platform modules; only the platform
modules import their chat SDK; the independence contract names the new
modules. The setup layer now sits above bot (it reads the registry).

Behaviour is unchanged, including where the platforms differ (access
rules, reply formats, media handling): the parity test passes with only
its module paths updated, and `memclaw` prints the same start-up errors.
@emanuilo
emanuilo force-pushed the refactor/2-chat-pipeline branch from 1d6d9c2 to 0ffa834 Compare November 22, 2026 19:21
@emanuilo
emanuilo changed the base branch from refactor/1-settings to main November 22, 2026 19:22
@emanuilo emanuilo closed this Nov 22, 2026
@emanuilo emanuilo reopened this Nov 22, 2026
@emanuilo
emanuilo merged commit e5c177a into main Nov 22, 2026
5 checks passed
@emanuilo
emanuilo deleted the refactor/2-chat-pipeline branch November 22, 2026 20:12
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