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
5 changes: 5 additions & 0 deletions .changeset/stream-source-cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chat": patch
---

Close the source stream passed to `thread.post()` when an adapter's `stream()` stops reading early. The stream wrappers given to `stream()` and to the post and edit fallback had `next()` but no `return()`, so leaving the loop early (a `break`, a thrown error, or a failed post) never reached the cleanup in the abort handling, and the source generator stayed open with its model request still running. Both wrappers are now async generators, which pass `return()` through to the source.
110 changes: 110 additions & 0 deletions packages/chat/src/thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,116 @@ describe("ThreadImpl", () => {
});
});

describe("source stream cleanup", () => {
let closed: boolean;

async function* endless(): AsyncIterable<string> {
try {
while (true) {
yield "word ";
}
} finally {
closed = true;
}
}

function createThread(adapter: Adapter, signal?: AbortSignal): ThreadImpl {
return new ThreadImpl({
id: "slack:C123:1234.5678",
adapter,
channelId: "C123",
stateAdapter: createMockState(),
fallbackStreamingPlaceholderText: null,
signal,
});
}

beforeEach(() => {
closed = false;
});

it("closes the source when the adapter stream throws mid loop", async () => {
const adapter = createMockAdapter();
adapter.stream = async (_threadId, textStream) => {
for await (const _ of textStream) {
throw new Error("too long");
}
return null;
};

await expect(createThread(adapter).post(endless())).rejects.toThrow(
"too long"
);
expect(closed).toBe(true);
});

it("closes the source when the adapter stream breaks early", async () => {
const adapter = createMockAdapter();
adapter.stream = async (_threadId, textStream) => {
let count = 0;
for await (const _ of textStream) {
count++;
if (count === 2) {
break;
}
}
return { id: "msg-stream", threadId: "t1", raw: null };
};

const sent = await createThread(adapter).post(endless());

expect(closed).toBe(true);
expect(sent.text).toBe("word word");
});

it("closes the source when the fallback post fails", async () => {
const adapter = createMockAdapter();
adapter.stream = undefined;
vi.mocked(adapter.postMessage).mockRejectedValueOnce(
new Error("post failed")
);

await expect(createThread(adapter).post(endless())).rejects.toThrow(
"post failed"
);
expect(closed).toBe(true);
});

it("closes the source when the thread is aborted", async () => {
const controller = new AbortController();
const adapter = createMockAdapter();
adapter.stream = async (_threadId, textStream) => {
for await (const _ of textStream) {
controller.abort();
}
return { id: "msg-stream", threadId: "t1", raw: null };
};

await createThread(adapter, controller.signal).post(endless());

expect(closed).toBe(true);
});

it("stops early on a source iterator without return", async () => {
const source: AsyncIterable<string> = {
[Symbol.asyncIterator]: () => ({
next: async () => ({ done: false, value: "word " }),
}),
};
const adapter = createMockAdapter();
adapter.stream = async (_threadId, textStream) => {
for await (const _ of textStream) {
break;
}
return { id: "msg-stream", threadId: "t1", raw: null };
};

const sent = await createThread(adapter).post(source);

expect(sent.text).toBe("word");
});
});

describe("streaming with StreamChunk objects", () => {
let thread: ThreadImpl;
let mockAdapter: Adapter;
Expand Down
83 changes: 35 additions & 48 deletions packages/chat/src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,37 @@ async function* withoutReasoning(
}
}

function chunkText(chunk: string | StreamChunk): string | undefined {
if (typeof chunk === "string") {
return chunk;
}
if (chunk.type === "markdown_text") {
return chunk.text;
}
return undefined;
}

async function* tapText(
source: AsyncIterable<string | StreamChunk>,
onText: (text: string) => void
): AsyncIterable<string | StreamChunk> {
for await (const chunk of source) {
onText(chunkText(chunk) ?? "");
yield chunk;
}
}

async function* textOnly(
source: AsyncIterable<string | StreamChunk>
): AsyncIterable<string> {
for await (const chunk of source) {
const text = chunkText(chunk);
if (text !== undefined) {
yield text;
}
}
}

async function* takeUntilAborted<T>(
source: AsyncIterable<T>,
signal: AbortSignal
Expand Down Expand Up @@ -839,29 +870,10 @@ export class ThreadImpl<TState = Record<string, unknown>>

// Use adapter-provided streaming if available.
if (adapter.stream) {
// Wrap stream to collect accumulated text while passing through to adapter.
// StreamChunk objects are passed through; only plain strings are accumulated.
let accumulated = "";
const wrappedStream: AsyncIterable<string | StreamChunk> = {
[Symbol.asyncIterator]: () => {
const iterator = textStream[Symbol.asyncIterator]();
return {
async next() {
const result = await iterator.next();
if (!result.done) {
const value = result.value;
if (typeof value === "string") {
accumulated += value;
} else if (value.type === "markdown_text") {
accumulated += value.text;
}
// task_update, plan_update, and reasoning chunks don't contribute to accumulated text
}
return result;
},
};
},
};
const wrappedStream = tapText(textStream, (text) => {
accumulated += text;
});

let raw: RawMessage<unknown> | null;
try {
Expand All @@ -886,32 +898,7 @@ export class ThreadImpl<TState = Record<string, unknown>>
}
}

// Fallback: post + edit with throttling.
// Extract only text content from the mixed stream for adapters without native streaming.
const textOnlyStream: AsyncIterable<string> = {
[Symbol.asyncIterator]: () => {
const iterator = textStream[Symbol.asyncIterator]();
return {
async next(): Promise<IteratorResult<string>> {
while (true) {
const result = await iterator.next();
if (result.done) {
return { value: undefined as unknown as string, done: true };
}
const value = result.value;
if (typeof value === "string") {
return { value, done: false };
}
if (value.type === "markdown_text") {
return { value: value.text, done: false };
}
// Skip non-text chunks (task_update, plan_update, reasoning) in fallback mode
}
},
};
},
};
return this.fallbackStream(textOnlyStream, options);
return this.fallbackStream(textOnly(textStream), options);
}

/**
Expand Down
Loading