Skip to content

Implement periodic text streaming for Telegram message updates#27

Draft
maksyms wants to merge 3 commits intoNachoSEO:mainfrom
maksyms:claude/fix-progress-truncation-lBX5V
Draft

Implement periodic text streaming for Telegram message updates#27
maksyms wants to merge 3 commits intoNachoSEO:mainfrom
maksyms:claude/fix-progress-truncation-lBX5V

Conversation

@maksyms
Copy link
Copy Markdown
Contributor

@maksyms maksyms commented Mar 24, 2026

Summary

Refactored the message streaming mechanism to use a periodic timer-based approach for flushing accumulated text to Telegram, replacing the previous throttling logic. This improves responsiveness and provides more consistent updates during long-running operations.

Key Changes

  • Text streaming timer: Added textStreamInterval to StreamState that triggers flushTextStream() every 3 seconds (configurable via TEXT_STREAM_INTERVAL_MS)
  • New flushTextStream() method: Periodically sends accumulated text content to Telegram with a cursor indicator, handling both plain text and terminal UI modes
  • Improved state tracking:
    • Renamed lastUpdate to lastEditMs for clarity
    • Added lastEditedContent to track what was last sent to avoid redundant edits
    • Added finishing flag to prevent race conditions during cleanup
  • Rate limiting: Maintains existing rate limit handling with rateLimitedUntil checks
  • Cleanup: Properly clear the text stream interval in finishStreaming() and error handlers
  • Terminal mode integration: When a tool operation is active, delegates to flushTerminalUpdate() instead of sending plain text
  • Session management: Clear Claude session ID when working directory changes, as the Agent SDK session is bound to the original working directory
  • UI improvement: Increased URL truncation length from 40 to 60 characters in terminal renderer for better readability

Implementation Details

  • The periodic timer starts immediately when startStreaming() is called and runs independently of message edits
  • Text content is accumulated in updateStream() and flushed by the timer, decoupling accumulation from transmission
  • Long content (>3500 chars) uses a sliding window display with ellipsis to stay within Telegram limits
  • A cursor character (█) is appended to indicate the response is still generating
  • The finishing flag ensures in-flight timer callbacks bail out gracefully during shutdown

https://claude.ai/code/session_01DVUNnHodU7bpSyqAycc48A

maksyms and others added 3 commits March 23, 2026 12:24
When switching projects via /project, setWorkingDirectory() preserved the
Claude Agent SDK session ID from the previous project. The subsequent call
to clearConversation() only cleared the chatSessionIds map in agent.ts,
but sendToAgent() falls back to session.claudeSessionId — which still
held the stale ID.

This caused the Agent SDK to attempt resuming a session created for a
different cwd, resulting in error_during_execution on the first message
after every project switch. The second message would work because the
error handler cleared the stale session.

Fix: clear claudeSessionId in setWorkingDirectory() when the directory
actually changes, since the Agent SDK session is bound to the original
cwd and cannot be resumed with a different one.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Streaming mode previously accumulated text in memory but never showed
it to the user during generation, making it identical to wait mode.

Now a 3-second periodic timer edits the placeholder message with
accumulated plain text as Claude generates it. Final delivery remains
formatted MarkdownV2. Tool operations (terminal UI) interleave
naturally — tool status takes priority when active.

Key changes:
- New flushTextStream() method with sliding window for long content
- Cursor indicator while generating
- Shared rate limit between text streaming and terminal UI
- Timer cleanup in finishStreaming() and cancelStreaming()
- Lowered edit throttle from 10s to 3s (TEXT_STREAM_INTERVAL_MS)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…nal message

Two bugs introduced by the incremental text streaming commit:

1. Spinner frozen: flushTextStream() delegated to flushTerminalUpdate()
   every 3s but never advanced spinnerIndex, so the Braille spinner showed
   the same frame indefinitely. Now spinnerIndex is incremented each time
   flushTextStream() delegates, so the spinner cycles with every timer tick.

2. Truncated status as final message: flushTextStream() is async and could
   race with finishStreaming() — an in-flight flush could overwrite the
   final formatted response (or vice versa) leaving the "Searching web …
   disc…" status line as the last visible message. Fixed by setting
   state.finishing = true before any async work in finishStreaming(), which
   causes concurrent flushTextStream() calls to bail out immediately.

   Also raised truncateUrl() default maxLen from 40 → 60 chars so search
   queries are less aggressively clipped in the status line.

https://claude.ai/code/session_01DVUNnHodU7bpSyqAycc48A
@maksyms maksyms marked this pull request as draft March 24, 2026 08:45
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.

2 participants