Skip to content

Commit 63293c0

Browse files
committed
refactor(tui): share terminal error cleanup
1 parent e13503a commit 63293c0

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

src/tui/tui-event-handlers.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,33 @@ export function createEventHandlers(context: EventHandlerContext) {
358358
void refreshSessionInfo?.();
359359
};
360360

361-
const renderTerminalLifecycleError = (runId: string, errorMessage: string) => {
361+
const renderTerminalRunError = (params: {
362+
runId: string;
363+
errorMessage: string;
364+
requireActiveOrPending?: boolean;
365+
}): boolean => {
366+
const { runId, errorMessage } = params;
362367
const wasActiveRun = state.activeChatRunId === runId;
363-
if (!wasActiveRun && state.pendingChatRunId !== runId) {
364-
return;
368+
if (
369+
params.requireActiveOrPending === true &&
370+
!wasActiveRun &&
371+
state.pendingChatRunId !== runId
372+
) {
373+
return false;
365374
}
366375
const renderedError = formatRawAssistantErrorForUi(errorMessage);
367376
chatLog.dismissPendingSystem(runId);
368377
chatLog.addSystem(resolveAuthErrorHint(errorMessage) ?? `run error: ${renderedError}`);
369378
noteFinalizedRun(runId, { displayedFinal: true });
370379
terminateRun({ runId, wasActiveRun, status: "error" });
371380
maybeRefreshHistoryForRun(runId);
381+
return true;
382+
};
383+
384+
const renderTerminalLifecycleError = (runId: string, errorMessage: string) => {
385+
if (!renderTerminalRunError({ runId, errorMessage, requireActiveOrPending: true })) {
386+
return;
387+
}
372388
tui.requestRender(true);
373389
};
374390

@@ -618,12 +634,10 @@ export function createEventHandlers(context: EventHandlerContext) {
618634
}
619635
if (evt.state === "error") {
620636
forgetLocalBtwRunId?.(evt.runId);
621-
const wasActiveRun = state.activeChatRunId === evt.runId;
622-
const errorMessage = evt.errorMessage ?? "unknown";
623-
const renderedError = formatRawAssistantErrorForUi(errorMessage);
624-
chatLog.addSystem(resolveAuthErrorHint(errorMessage) ?? `run error: ${renderedError}`);
625-
terminateRun({ runId: evt.runId, wasActiveRun, status: "error" });
626-
maybeRefreshHistoryForRun(evt.runId);
637+
renderTerminalRunError({
638+
runId: evt.runId,
639+
errorMessage: evt.errorMessage ?? "unknown",
640+
});
627641
}
628642
tui.requestRender();
629643
};

0 commit comments

Comments
 (0)