Skip to content

Commit 5efba13

Browse files
Cleanup inactive reconnect state entries on access
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 227290b commit 5efba13

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,33 @@ describe("TriggerChatTransport", function () {
476476
expect(stream).toBeNull();
477477
});
478478

479+
it("removes inactive run entries during reconnect attempts", async function () {
480+
const runStore = new TrackedRunStore();
481+
runStore.set({
482+
chatId: "chat-inactive",
483+
runId: "run_inactive",
484+
publicAccessToken: "pk_inactive",
485+
streamKey: "chat-stream",
486+
lastEventId: "10-0",
487+
isActive: false,
488+
});
489+
490+
const transport = new TriggerChatTransport({
491+
task: "chat-task",
492+
stream: "chat-stream",
493+
accessToken: "pk_trigger",
494+
runStore,
495+
});
496+
497+
const stream = await transport.reconnectToStream({
498+
chatId: "chat-inactive",
499+
});
500+
501+
expect(stream).toBeNull();
502+
expect(runStore.deleteCalls).toContain("chat-inactive");
503+
expect(runStore.get("chat-inactive")).toBeUndefined();
504+
});
505+
479506
it("supports custom payload mapping and trigger options resolver", async function () {
480507
let receivedTriggerBody: Record<string, unknown> | undefined;
481508
let receivedResolverChatId: string | undefined;

packages/ai/src/chatTransport.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ export class TriggerChatTransport<
195195
): Promise<ReadableStream<UIMessageChunk> | null> {
196196
const runState = await this.runStore.get(options.chatId);
197197

198-
if (!runState || !runState.isActive) {
198+
if (!runState) {
199+
return null;
200+
}
201+
202+
if (!runState.isActive) {
203+
await this.runStore.delete(options.chatId);
199204
return null;
200205
}
201206

0 commit comments

Comments
 (0)