Telegram: support the draft stop button (can_stop, keep_on_stop)

Open
#1,139 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
api

Research direction

Start with nativeDraftStream and the Telegram update routing and types described in the issue; trace how draft IDs are created and how the stream reads textStream. Implement the first roadmap step: stop the matching in-process stream and save its partial text, then update the Telegram docs and add a changeset. The issue names no specific source or test files, and the later bot notification and cross-instance behavior remain open questions.

Written by the indexing model from the issue text.

Description

enhancement
Problem Statement

When nativeStreaming: true is set, the Telegram adapter streams to private chats with sendRichMessageDraft (or sendMessageDraft as a fallback) and saves the result with sendRichMessage. Users have no way to stop a long answer while it streams.

Both draft methods accept can_stop, which shows the user a stop button. When the user presses it, Telegram sends the bot a stopped_message_generation update (MessageGenerationStopped, with chat, optional message_thread_id, and draft_id). keep_on_stop keeps the partial draft visible after the press, for a short time or until the bot sends a message.

The adapter doesn't send can_stop or keep_on_stop, and it doesn't handle stopped_message_generation, so the stream keeps running until the model finishes.

Proposed Solution

Add opt-in stop support to native draft streaming in @chat-adapter/telegram.

  • Config: an option such as streamStopButton: true, off by default. It applies only when nativeStreaming is on, because post-and-edit streams (the default) don't use drafts.
  • Sending: nativeDraftStream passes can_stop: true and keep_on_stop: true to sendRichMessageDraft, and to sendMessageDraft in the plain fallback.
  • Receiving: add stopped_message_generation to the update types the adapter handles, and to TELEGRAM_DEFAULT_ALLOWED_UPDATES, which the adapter sends as an explicit list in business mode.
  • Stopping: match the update to the running stream by chat id, message_thread_id, and draft_id, then stop reading textStream.
  • Saving: send the text received so far with sendRichMessage, as when a stream ends normally, so the partial answer stays in the chat. keep_on_stop keeps the draft visible until then, so the message doesn't flicker.
Open questions
  1. How does the bot learn about the stop? Stopping the adapter's read loop doesn't cancel the model request. StreamOptions.signal already lets the bot cancel a stream, but the adapter can't abort a signal the bot owns. Options:
    • The adapter aborts an AbortSignal it exposes for the stream, which the bot passes to streamText.
    • A new handler, such as onStreamStop, receives the thread and the partial text.
  2. How does a stop reach the right process? On serverless deployments, the webhook with stopped_message_generation can arrive at a different instance from the one running the stream. One approach is to write a stop flag to the state adapter and check it on each draft flush.
  3. Draft ids come from a per-process counter (createDraftId), so two instances can use the same draft_id in one chat. Should draft ids become random, or is matching on the chat plus the id enough?
Roadmap
  1. Stop button and update handling. Add the option, send can_stop and keep_on_stop, add the MessageGenerationStopped type to types.ts, route stopped_message_generation, stop the read loop, and save the partial text. Cover a stop arriving in the same process. Update the Telegram docs page and add a changeset.
  2. Bot notification and cross-instance stops. Implement the answers to open questions 1 and 2.
Alternatives Considered
  • Handling stopped_message_generation in bot code. The adapter doesn't route the update, so a bot would need to subclass TelegramAdapter and override processUpdate, and it couldn't reach the stream's draft id.
  • Stopping only the read loop, without telling the bot. The stream ends in the chat, but the model keeps generating tokens.
Use Case
import { Chat } from "chat";
import { createTelegramAdapter } from "@chat-adapter/telegram";
import { createMemoryState } from "@chat-adapter/state-memory";
import { streamText } from "ai";
import { model } from "./model"; // any AI SDK model

const bot = new Chat({
  userName: "mybot",
  adapters: {
    telegram: createTelegramAdapter({
      nativeStreaming: true,
      // Proposed option
      streamStopButton: true,
    }),
  },
  state: createMemoryState(),
});

bot.onNewMention(async (thread, message) => {
  const result = streamText({ model, prompt: message.text });
  // Private chat: the draft shows a stop button. Pressing it ends the
  // stream and keeps the text received so far.
  await thread.post(result.fullStream);
});
Priority

Important

Contribution
  • I am willing to help implement this feature
Additional Context
Dominant language
TypeScript
Stars
2.5k
Forks
355
Avg merge
2d 15h
Merged PRs (30d)
42

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel/chat

All issues in vercel/chat

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.