Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/slack-streaming-keepalive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@chat-adapter/slack": patch
---

Add streaming keepalive to prevent `message_not_in_streaming_state` errors

Slack's streaming API expires the session after ~5 minutes of inactivity. When the upstream `textStream` iterable pauses for extended periods (e.g. during long-running agent tool calls), the session expires and all subsequent `append` or `stop` calls fail with `message_not_in_streaming_state`.

The fix races each chunk from the text stream against a 2-minute keepalive timer. If no chunk arrives within 2 minutes, a zero-width-space is appended to keep the Slack session alive. No chunks are ever dropped — the same pending promise is re-raced after each keepalive.
29 changes: 28 additions & 1 deletion packages/adapter-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,34 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
lastAppended = committable;
};

for await (const chunk of textStream) {
// Race each chunk against a keepalive timer. Slack expires the streaming
// session after ~5 min of inactivity; we send a zero-width-space append
// every 2 min to keep it alive during long-running agent turns.
const KEEPALIVE_MS = 120_000;
const iter = textStream[Symbol.asyncIterator]();
let pending: Promise<IteratorResult<string | StreamChunk>> | null = null;

// eslint-disable-next-line no-constant-condition
while (true) {
if (!pending) pending = iter.next();

const raced = await Promise.race([
pending.then((r) => ({ kind: "value" as const, result: r })),
new Promise<{ kind: "keepalive" }>((r) =>
setTimeout(() => r({ kind: "keepalive" }), KEEPALIVE_MS)
),
]);

if (raced.kind === "keepalive") {
// Send an invisible append to keep the Slack streaming session alive
await flushMarkdownDelta("\u200B");
continue;
}

pending = null;
if (raced.result.done) break;

const chunk = raced.result.value;
if (typeof chunk === "string") {
await pushTextAndFlush(chunk);
} else if (chunk.type === "markdown_text") {
Expand Down
Loading