Skip to content

Commit e97fc5c

Browse files
Refactor run-state cleanup into shared transport helper
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent a1d32c4 commit e97fc5c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

packages/ai/src/chatTransport.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ export class TriggerChatTransport<
235235
try {
236236
stream = await this.fetchRunStream(runState, options.abortSignal);
237237
} catch (error) {
238-
runState.isActive = false;
239-
await this.runStore.set(runState);
240-
await this.runStore.delete(runState.chatId);
238+
await this.markRunInactiveAndDelete(runState);
241239
await this.reportError({
242240
phase: "streamSubscribe",
243241
chatId: runState.chatId,
@@ -268,9 +266,7 @@ export class TriggerChatTransport<
268266
try {
269267
stream = await this.fetchRunStream(runState, undefined, runState.lastEventId);
270268
} catch (error) {
271-
runState.isActive = false;
272-
await this.runStore.set(runState);
273-
await this.runStore.delete(options.chatId);
269+
await this.markRunInactiveAndDelete(runState);
274270
await this.reportError({
275271
phase: "reconnect",
276272
chatId: runState.chatId,
@@ -347,16 +343,12 @@ export class TriggerChatTransport<
347343

348344
const runState = await this.runStore.get(chatId);
349345
if (runState) {
350-
runState.isActive = false;
351-
await this.runStore.set(runState);
352-
await this.runStore.delete(chatId);
346+
await this.markRunInactiveAndDelete(runState);
353347
}
354348
} catch (error) {
355349
const runState = await this.runStore.get(chatId);
356350
if (runState) {
357-
runState.isActive = false;
358-
await this.runStore.set(runState);
359-
await this.runStore.delete(chatId);
351+
await this.markRunInactiveAndDelete(runState);
360352
await this.reportError({
361353
phase: "consumeTrackingStream",
362354
chatId: runState.chatId,
@@ -387,6 +379,12 @@ export class TriggerChatTransport<
387379
return `${normalizedBaseUrl}/realtime/v1/streams/${encodedRunId}/${encodedStreamKey}`;
388380
}
389381

382+
private async markRunInactiveAndDelete(runState: TriggerChatRunState) {
383+
runState.isActive = false;
384+
await this.runStore.set(runState);
385+
await this.runStore.delete(runState.chatId);
386+
}
387+
390388
private async reportError(event: TriggerChatTransportError) {
391389
if (!this.onError) {
392390
return;

0 commit comments

Comments
 (0)