(workspaceId: string)
| 7998 | } |
| 7999 | |
| 8000 | async resetContext(workspaceId: string): Promise<Result<"reset" | "noop">> { |
| 8001 | if (this.resettingContextWorkspaces.has(workspaceId)) { |
| 8002 | return Err("Context reset is already in progress for this workspace."); |
| 8003 | } |
| 8004 | |
| 8005 | this.resettingContextWorkspaces.add(workspaceId); |
| 8006 | try { |
| 8007 | const session = this.sessions.get(workspaceId); |
| 8008 | if (session?.isBusy() || this.aiService.isStreaming(workspaceId)) { |
| 8009 | return Err( |
| 8010 | "Cannot reset context while a turn is active. Press Esc to stop the stream first." |
| 8011 | ); |
| 8012 | } |
| 8013 | |
| 8014 | if (this.hasPendingQueuedOrPreparingTurn(workspaceId)) { |
| 8015 | return Err( |
| 8016 | "Cannot reset context while queued user input is pending. Send or clear the queued message first." |
| 8017 | ); |
| 8018 | } |
| 8019 | |
| 8020 | const historyResult = await this.historyService.getHistoryFromLatestBoundary(workspaceId); |
| 8021 | if (!historyResult.success) { |
| 8022 | return Err(`Failed to read active context before reset: ${historyResult.error}`); |
| 8023 | } |
| 8024 | |
| 8025 | const activeContextMessages = sliceMessagesForProviderFromLatestContextBoundary( |
| 8026 | historyResult.data |
| 8027 | ); |
| 8028 | if (!hasProviderEligibleMessages(activeContextMessages)) { |
| 8029 | return Ok("noop"); |
| 8030 | } |
| 8031 | |
| 8032 | const boundaryMessage = createMuxMessage( |
| 8033 | createContextResetBoundaryMessageId(), |
| 8034 | "assistant", |
| 8035 | "", |
| 8036 | { |
| 8037 | timestamp: Date.now(), |
| 8038 | contextBoundaryKind: CONTEXT_BOUNDARY_KINDS.RESET, |
| 8039 | } |
| 8040 | ); |
| 8041 | |
| 8042 | const appendResult = await this.historyService.appendToHistory(workspaceId, boundaryMessage); |
| 8043 | if (!appendResult.success) { |
| 8044 | return Err(`Failed to append context reset boundary: ${appendResult.error}`); |
| 8045 | } |
| 8046 | |
| 8047 | const typedBoundaryMessage = { ...boundaryMessage, type: "message" as const }; |
| 8048 | if (session) { |
| 8049 | session.emitChatEvent(typedBoundaryMessage); |
| 8050 | } else { |
| 8051 | this.emit("chat", { workspaceId, message: typedBoundaryMessage }); |
| 8052 | } |
| 8053 | |
| 8054 | try { |
| 8055 | await this.workspaceGoalService?.requireUserAcknowledgment(workspaceId); |
| 8056 | } catch (error) { |
| 8057 | log.error("Failed to require goal acknowledgment after context reset:", error); |
no test coverage detected