tgcli
tgcli copied to clipboard
refactor: simplify the operations/command layer (#49, #43)
Cleanup of the operations/command layer the warm-backend work produced — no user-visible behavior change (same output incl --json, same errors/exit codes). Addresses #49 and #43.
Changes
- Invoke-first (perf).
runOperationnow callsinvokedirectly (one loopback round-trip on the warm path instead of ping+invoke). Only aServerUnavailableError(nocontrol.json/ refused loopback connection — detected viaisFetchConnectionFailure) triggersensureServer+ a single retry. A 4xx/5xx op error, a relayedsendError, and a request timeout/abort are NOT connection failures and never spawn a server (preserves the send-timeout contract). - Source-resolution dedup.
resolveFromSource({ source, fetchLive, fetchArchive })andselectMessageResults(...)collapse the duplicated live/both/archive/live-fallback logic acrossmessagesGet/messagesContext/messagesList/messagesSearch. Identical behavior. - Generic passthrough + thinner handlers (subsumes #43). A small
PASSTHROUGHStable generates 16 one-line ops viabuildPassthroughOps; only composite ops stay hand-written. All 44 ops and their result shapes are preserved (the MCP tools and/control/invokedepend on them). The threesend*handlers sharebuildCommonSendArgsinstead of each rebuilding the flag map (same validation order → same first-error). - Audits. Source consistency: every single-source result comes wholly from one source;
bothmerges viamergeMessageSetspreserving each row'ssource;archive→livefallback surfacesusedLiveFallback. No mixing found. TG calls: one follow-up reported (not changed, since it affects which labels appear) —resolveLiveMetadataissues an extragetPeerMetadatawhenusernameisn't cached even though the fetch already returnspeerTitle; candidate to cache/skip later.
Tests
npm test → 349 passing (336 + 13 new: invoke-first vs spawn-on-connection-failure incl. op-error-does-not-spawn, the source-resolution helper across all source modes, the generic passthrough forwarding, handlers passing args through). No node_modules committed; comments behavioral.
Note
#49's "minimize TG API calls" is covered except the one reported resolveLiveMetadata round-trip (a safe-to-defer follow-up). #43 (thin handlers) is addressed via the passthrough + shared send args.