diff --git a/docs/superpowers/specs/2026-09-26-telegram-attachments-design.md b/docs/superpowers/specs/2026-09-26-telegram-attachments-design.md index d87e155..e871f8b 100644 --- a/docs/superpowers/specs/2026-09-26-telegram-attachments-design.md +++ b/docs/superpowers/specs/2026-09-26-telegram-attachments-design.md @@ -30,7 +30,7 @@ contract to reach "this session's chat". | Image documents | JPEG/PNG/WebP/GIF sent "as file" are saved like any document **and** attached to the prompt as `ImageContent` when ≤ `InlineImageMaxBytes` (3.75 MB, so base64 stays under the 5 MB image cap providers enforce) | | Speech to text | A **host service** (`Speech/ISpeechToText`, `OpenAiSpeechToText`), not an abstraction: it normalises input before a prompt exists, and the core only ever sees text | | STT model | `gpt-4o-transcribe` by default, configurable. Off when no API key is configured; voice then gets a clear "not configured" reply | -| Transcript echo | `🎤 ` goes to the chat before the run starts, cut to Telegram's 4096-character limit | +| Transcript echo | `🎤 ` goes to the chat before the run starts, cut to 1024 characters | | Ordering | Inbound preparation (download, save, transcribe) runs as content steps inside `InboundComposer.ComposeAsync`, under the message's `ChatInbox` lease (message-context spec). Polling never blocks, and parts merge in Telegram order whatever order they finish in. Notices, the `🎤` echo and outbound files go through the chat renderer's queue (`RunInOrderAsync`) so they stay in order with rendered text | | Commands / answers | A message with an attachment is never a command and never answers a pending `InputAsync` question. Its caption is prompt text. This extends today's photo rule | @@ -252,7 +252,7 @@ Router changes (`UpdateRouter.OnMessageAsync`), on top of the message-context sp That includes a cancellation the message's own token didn't cause, such as the `HttpClient` timeout; only a shutdown (the caller's token) ends composing silently. The same rule holds for downloads, and `UpdateRouter` treats any other `OperationCanceledException` from `ComposeAsync` as a failure (`I could not read that message.`). -5. Echo `🎤 ` as plain text, cut to 4096 characters with `…`. +5. Echo `🎤 ` as plain text, cut to 1024 characters with `…` (the prompt keeps all of it). 6. Prompt text: the caption (if any), then `[Voice message] `. For `Audio`, the file is also saved to the inbox like a document and gets its own `[File received: …]` line: it's something the user might want processed, not just heard. diff --git a/docs/telegram-bot.md b/docs/telegram-bot.md index 63a9174..c2ce45f 100644 --- a/docs/telegram-bot.md +++ b/docs/telegram-bot.md @@ -132,7 +132,7 @@ question in that chat (for example from an extension) replaces the pending promp GIF) up to 3.75 MB (`Telegram:InlineImageMaxBytes`) is also shown to the model; a larger one, or one whose contents aren't really one of those formats, reaches it as a path only. - **Voice messages and audio files** are transcribed with OpenAI (`gpt-4o-transcribe` by default). The bot echoes - `🎤 ` and the agent gets `[Voice message] ` (an audio file also gets its `[File received: …]` + `🎤 ` (cut to 1024 characters) and the agent gets `[Voice message] ` (an audio file also gets its `[File received: …]` line). Without an API key the bot replies with how to set one up (`OPENAI_API_KEY`). - Telegram lets bots download at most 20 MB. A bigger file is refused with a note, and nothing reaches the agent. - A message with an attachment is never a command and never answers a pending question: its caption is prompt text. diff --git a/hosts/Volcanic.AgenticCore.Telegram/Inbound/InboundMedia.cs b/hosts/Volcanic.AgenticCore.Telegram/Inbound/InboundMedia.cs index 1daf281..65dd788 100644 --- a/hosts/Volcanic.AgenticCore.Telegram/Inbound/InboundMedia.cs +++ b/hosts/Volcanic.AgenticCore.Telegram/Inbound/InboundMedia.cs @@ -20,6 +20,9 @@ public sealed class InboundMedia(TelegramSender sender, TelegramOptions options, /// Sent for a voice note or audio file when no speech-to-text service is configured. public const string SpeechNotConfiguredNotice = "Voice messages need speech-to-text: set OPENAI_API_KEY (or Telegram:Speech:ApiKey) on the bot host."; + /// The longest 🎤 echo, in UTF-16 code units; a long transcript is cut so it doesn't flood the chat. + public const int MaxEchoLength = 1024; + /// Sent when the transcript is empty. public const string NoWordsNotice = "I couldn't make out any words in that voice message."; @@ -170,10 +173,20 @@ public sealed class InboundMedia(TelegramSender sender, TelegramOptions options, await chat.Renderer.SayAsync(NoWordsNotice).ConfigureAwait(false); return; } - await chat.Renderer.SayAsync("🎤 " + transcript).ConfigureAwait(false); // plain text, cut to 4096 by the sender + await chat.Renderer.SayAsync(Echo(transcript)).ConfigureAwait(false); // plain text draft.Body.Add($"[Voice message] {transcript}"); } + /// The 🎤 … echo, cut to characters with … (the model gets all of it). + internal static string Echo(string transcript) + { + var echo = "🎤 " + transcript; + if (echo.Length <= MaxEchoLength) return echo; + var cut = MaxEchoLength - 1; + if (char.IsHighSurrogate(echo[cut - 1])) cut--; // don't split an emoji + return echo[..cut] + "…"; + } + /// /// The name an audio file is sent to speech to text under: OpenAI picks the format from the extension, so it's an /// ASCII audio.<ext> from the original extension when OpenAI knows it, else from the MIME type. diff --git a/tests/Volcanic.AgenticCore.Telegram.Tests/VoiceChatTests.cs b/tests/Volcanic.AgenticCore.Telegram.Tests/VoiceChatTests.cs index c4527b4..9a4c820 100644 --- a/tests/Volcanic.AgenticCore.Telegram.Tests/VoiceChatTests.cs +++ b/tests/Volcanic.AgenticCore.Telegram.Tests/VoiceChatTests.cs @@ -31,7 +31,7 @@ public sealed class VoiceChatTests } [Fact] - public async Task A_long_transcript_echo_is_cut_to_4096_characters() + public async Task A_long_transcript_echo_is_cut_to_1024_characters() { var stt = new FakeSpeechToText(new string('a', 5000)); await using var host = new ChatHost(speech: stt); @@ -42,7 +42,7 @@ public sealed class VoiceChatTests await host.SettleAsync(); var echo = host.Bot.Sent.Single(s => s.Text.StartsWith("🎤", StringComparison.Ordinal)).Text; - Assert.Equal(4096, echo.Length); + Assert.Equal(1024, echo.Length); Assert.EndsWith("…", echo, StringComparison.Ordinal); Assert.Contains(new string('a', 5000), Prompt(host), StringComparison.Ordinal); // the model gets all of it }