- Fix scrambled text when typing in Base URL fields: debounced input hook now ignores echo round-trips of its own saves and defers unexpected external updates during an edit grace period - Serialize API configuration saves through a queue so rapid successive updates are applied in order on the backend - Fix Base URL input collapsing to tiny width: Radix TooltipTrigger now renders a full-width block wrapper instead of the default shrink-to-fit button - Trim Base URL values only on commit instead of every keystroke to prevent cursor jumps - Skip redundant save on mount and flush uncommitted edits on unmount - Bump version to 4.3.1, update CHANGELOG and README
303 lines
11 KiB
TypeScript
303 lines
11 KiB
TypeScript
import CheckpointTracker from "@integrations/checkpoints/CheckpointTracker"
|
|
import { EventEmitter } from "events"
|
|
import getFolderSize from "get-folder-size"
|
|
import Mutex from "p-mutex"
|
|
import { findLastIndex } from "@/shared/array"
|
|
import { combineApiRequests } from "@/shared/combineApiRequests"
|
|
import { combineCommandSequences } from "@/shared/combineCommandSequences"
|
|
import { ClineMessage } from "@/shared/ExtensionMessage"
|
|
import { getApiMetrics } from "@/shared/getApiMetrics"
|
|
import { HistoryItem } from "@/shared/HistoryItem"
|
|
import { ClineStorageMessage } from "@/shared/messages/content"
|
|
import { Logger } from "@/shared/services/Logger"
|
|
import { extractUsageEntries } from "@/shared/usage-stats"
|
|
import { getCwd, getDesktopDir } from "@/utils/path"
|
|
import { ensureTaskDirectoryExists, saveApiConversationHistory, saveClineMessages } from "../storage/disk"
|
|
import { TaskState } from "./TaskState"
|
|
|
|
// Event types for clineMessages changes
|
|
export type ClineMessageChangeType = "add" | "update" | "delete" | "set"
|
|
|
|
export interface ClineMessageChange {
|
|
type: ClineMessageChangeType
|
|
/** The full array after the change */
|
|
messages: ClineMessage[]
|
|
/** The affected index (for add/update/delete) */
|
|
index?: number
|
|
/** The new/updated message (for add/update) */
|
|
message?: ClineMessage
|
|
/** The old message before change (for update/delete) */
|
|
previousMessage?: ClineMessage
|
|
/** The entire previous array (for set) */
|
|
previousMessages?: ClineMessage[]
|
|
}
|
|
|
|
// Strongly-typed event emitter interface
|
|
export interface MessageStateHandlerEvents {
|
|
clineMessagesChanged: [change: ClineMessageChange]
|
|
}
|
|
|
|
interface MessageStateHandlerParams {
|
|
taskId: string
|
|
ulid: string
|
|
taskIsFavorited?: boolean
|
|
updateTaskHistory: (historyItem: HistoryItem) => Promise<HistoryItem[]>
|
|
taskState: TaskState
|
|
checkpointManagerErrorMessage?: string
|
|
}
|
|
|
|
export class MessageStateHandler extends EventEmitter<MessageStateHandlerEvents> {
|
|
private apiConversationHistory: ClineStorageMessage[] = []
|
|
private clineMessages: ClineMessage[] = []
|
|
private taskIsFavorited: boolean
|
|
private checkpointTracker: CheckpointTracker | undefined
|
|
private updateTaskHistory: (historyItem: HistoryItem) => Promise<HistoryItem[]>
|
|
private taskId: string
|
|
private ulid: string
|
|
private taskState: TaskState
|
|
|
|
// Mutex to prevent concurrent state modifications (RC-4)
|
|
// Protects against data loss from race conditions when multiple
|
|
// operations try to modify message state simultaneously
|
|
// This follows the same pattern as Task.stateMutex for consistency
|
|
private stateMutex = new Mutex()
|
|
|
|
constructor(params: MessageStateHandlerParams) {
|
|
super()
|
|
this.taskId = params.taskId
|
|
this.ulid = params.ulid
|
|
this.taskState = params.taskState
|
|
this.taskIsFavorited = params.taskIsFavorited ?? false
|
|
this.updateTaskHistory = params.updateTaskHistory
|
|
}
|
|
|
|
/**
|
|
* Emit a clineMessagesChanged event with the change details
|
|
*/
|
|
private emitClineMessagesChanged(change: ClineMessageChange): void {
|
|
this.emit("clineMessagesChanged", change)
|
|
}
|
|
|
|
setCheckpointTracker(tracker: CheckpointTracker | undefined) {
|
|
this.checkpointTracker = tracker
|
|
}
|
|
|
|
/**
|
|
* Execute function with exclusive lock on message state
|
|
* Use this for ANY state modification to prevent race conditions
|
|
* This follows the same pattern as Task.withStateLock for consistency
|
|
*/
|
|
private async withStateLock<T>(fn: () => T | Promise<T>): Promise<T> {
|
|
return await this.stateMutex.withLock(fn)
|
|
}
|
|
|
|
getTaskId(): string {
|
|
return this.taskId
|
|
}
|
|
|
|
getApiConversationHistory(): ClineStorageMessage[] {
|
|
return this.apiConversationHistory
|
|
}
|
|
|
|
setApiConversationHistory(newHistory: ClineStorageMessage[]): void {
|
|
this.apiConversationHistory = newHistory
|
|
}
|
|
|
|
getClineMessages(): ClineMessage[] {
|
|
return this.clineMessages
|
|
}
|
|
|
|
setClineMessages(newMessages: ClineMessage[]) {
|
|
const previousMessages = this.clineMessages
|
|
this.clineMessages = newMessages
|
|
this.emitClineMessagesChanged({
|
|
type: "set",
|
|
messages: this.clineMessages,
|
|
previousMessages,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Internal method to save messages and update history (without mutex protection)
|
|
* This is used by methods that already hold the stateMutex lock
|
|
* Should NOT be called directly - use saveClineMessagesAndUpdateHistory() instead
|
|
*/
|
|
private async saveClineMessagesAndUpdateHistoryInternal(): Promise<void> {
|
|
try {
|
|
await saveClineMessages(this.taskId, this.clineMessages)
|
|
|
|
// combined as they are in ChatView
|
|
const apiMetrics = getApiMetrics(combineApiRequests(combineCommandSequences(this.clineMessages.slice(1))))
|
|
const taskMessage = this.clineMessages[0] // first message is always the task say
|
|
const lastRelevantMessage =
|
|
this.clineMessages[
|
|
findLastIndex(
|
|
this.clineMessages,
|
|
(message) => !(message.ask === "resume_task" || message.ask === "resume_completed_task"),
|
|
)
|
|
]
|
|
const lastModelInfo = [...this.apiConversationHistory].reverse().find((msg) => msg.modelInfo !== undefined)
|
|
const taskDir = await ensureTaskDirectoryExists(this.taskId)
|
|
let taskDirSize = 0
|
|
try {
|
|
// getFolderSize.loose silently ignores errors
|
|
// returns # of bytes, size/1000/1000 = MB
|
|
taskDirSize = await getFolderSize.loose(taskDir)
|
|
} catch (error) {
|
|
Logger.error("Failed to get task directory size:", taskDir, error)
|
|
}
|
|
const cwd = await getCwd(getDesktopDir())
|
|
const { usageEntries, apiRequestCount } = extractUsageEntries(
|
|
combineApiRequests(combineCommandSequences(this.clineMessages.slice(1))),
|
|
)
|
|
|
|
await this.updateTaskHistory({
|
|
id: this.taskId,
|
|
ulid: this.ulid,
|
|
ts: lastRelevantMessage.ts,
|
|
task: taskMessage.text ?? "",
|
|
tokensIn: apiMetrics.totalTokensIn,
|
|
tokensOut: apiMetrics.totalTokensOut,
|
|
cacheWrites: apiMetrics.totalCacheWrites,
|
|
cacheReads: apiMetrics.totalCacheReads,
|
|
totalCost: apiMetrics.totalCost,
|
|
apiRequests: apiRequestCount,
|
|
usageEntries,
|
|
size: taskDirSize,
|
|
shadowGitConfigWorkTree: await this.checkpointTracker?.getShadowGitConfigWorkTree(),
|
|
cwdOnTaskInitialization: cwd,
|
|
conversationHistoryDeletedRange: this.taskState.conversationHistoryDeletedRange,
|
|
isFavorited: this.taskIsFavorited,
|
|
checkpointManagerErrorMessage: this.taskState.checkpointManagerErrorMessage,
|
|
modelId: lastModelInfo?.modelInfo?.modelId,
|
|
})
|
|
} catch (error) {
|
|
Logger.error("Failed to save cline messages:", error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save cline messages and update task history (public API with mutex protection)
|
|
* This is the main entry point for saving message state from external callers
|
|
*/
|
|
async saveClineMessagesAndUpdateHistory(): Promise<void> {
|
|
return await this.withStateLock(async () => {
|
|
await this.saveClineMessagesAndUpdateHistoryInternal()
|
|
})
|
|
}
|
|
|
|
async addToApiConversationHistory(message: ClineStorageMessage) {
|
|
// Protect with mutex to prevent concurrent modifications from corrupting data (RC-4)
|
|
return await this.withStateLock(async () => {
|
|
this.apiConversationHistory.push(message)
|
|
await saveApiConversationHistory(this.taskId, this.apiConversationHistory)
|
|
})
|
|
}
|
|
|
|
async overwriteApiConversationHistory(newHistory: ClineStorageMessage[]): Promise<void> {
|
|
// Protect with mutex to prevent concurrent modifications from corrupting data (RC-4)
|
|
return await this.withStateLock(async () => {
|
|
this.apiConversationHistory = newHistory
|
|
await saveApiConversationHistory(this.taskId, this.apiConversationHistory)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Add a new message to clineMessages array with proper index tracking
|
|
* CRITICAL: This entire operation must be atomic to prevent race conditions (RC-4)
|
|
* The conversationHistoryIndex must be set correctly based on the current state,
|
|
* and the message must be added and saved without any interleaving operations
|
|
*/
|
|
async addToClineMessages(message: ClineMessage) {
|
|
return await this.withStateLock(async () => {
|
|
// these values allow us to reconstruct the conversation history at the time this cline message was created
|
|
// it's important that apiConversationHistory is initialized before we add cline messages
|
|
message.conversationHistoryIndex = this.apiConversationHistory.length - 1 // NOTE: this is the index of the last added message which is the user message, and once the clinemessages have been presented we update the apiconversationhistory with the completed assistant message. This means when resetting to a message, we need to +1 this index to get the correct assistant message that this tool use corresponds to
|
|
message.conversationHistoryDeletedRange = this.taskState.conversationHistoryDeletedRange
|
|
const index = this.clineMessages.length
|
|
this.clineMessages.push(message)
|
|
this.emitClineMessagesChanged({
|
|
type: "add",
|
|
messages: this.clineMessages,
|
|
index,
|
|
message,
|
|
})
|
|
await this.saveClineMessagesAndUpdateHistoryInternal()
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Replace the entire clineMessages array with new messages
|
|
* Protected by mutex to prevent concurrent modifications (RC-4)
|
|
*/
|
|
async overwriteClineMessages(newMessages: ClineMessage[]) {
|
|
return await this.withStateLock(async () => {
|
|
const previousMessages = this.clineMessages
|
|
this.clineMessages = newMessages
|
|
this.emitClineMessagesChanged({
|
|
type: "set",
|
|
messages: this.clineMessages,
|
|
previousMessages,
|
|
})
|
|
await this.saveClineMessagesAndUpdateHistoryInternal()
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Update a specific message in the clineMessages array
|
|
* The entire operation (validate, update, save) is atomic to prevent races (RC-4)
|
|
*/
|
|
async updateClineMessage(index: number, updates: Partial<ClineMessage>): Promise<void> {
|
|
return await this.withStateLock(async () => {
|
|
if (index < 0 || index >= this.clineMessages.length) {
|
|
throw new Error(`Invalid message index: ${index}`)
|
|
}
|
|
|
|
// Capture previous state before mutation
|
|
const previousMessage = { ...this.clineMessages[index] }
|
|
|
|
// Apply updates to the message
|
|
Object.assign(this.clineMessages[index], updates)
|
|
|
|
this.emitClineMessagesChanged({
|
|
type: "update",
|
|
messages: this.clineMessages,
|
|
index,
|
|
previousMessage,
|
|
message: this.clineMessages[index],
|
|
})
|
|
|
|
// Save changes and update history
|
|
await this.saveClineMessagesAndUpdateHistoryInternal()
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Delete a specific message from the clineMessages array
|
|
* The entire operation (validate, delete, save) is atomic to prevent races (RC-4)
|
|
*/
|
|
async deleteClineMessage(index: number): Promise<void> {
|
|
return await this.withStateLock(async () => {
|
|
if (index < 0 || index >= this.clineMessages.length) {
|
|
throw new Error(`Invalid message index: ${index}`)
|
|
}
|
|
|
|
// Capture the message before deletion
|
|
const previousMessage = this.clineMessages[index]
|
|
|
|
// Remove the message at the specified index
|
|
this.clineMessages.splice(index, 1)
|
|
|
|
this.emitClineMessagesChanged({
|
|
type: "delete",
|
|
messages: this.clineMessages,
|
|
index,
|
|
previousMessage,
|
|
})
|
|
|
|
// Save changes and update history
|
|
await this.saveClineMessagesAndUpdateHistoryInternal()
|
|
})
|
|
}
|
|
}
|