From ec33ef5e3a2940193d28c7ef9e5c4340124867c4 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Thu, 22 Aug 2024 04:20:33 -0400 Subject: [PATCH] Remove unnecessary trailing resume messages --- src/ClaudeDev.ts | 14 +++++++++++++- src/utils/array-helpers.ts | 17 +++++++++++++++++ src/utils/index.ts | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/utils/array-helpers.ts diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index 2973995..b9523e8 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -24,6 +24,7 @@ import { getApiMetrics } from "./shared/getApiMetrics" import { HistoryItem } from "./shared/HistoryItem" import { combineApiRequests } from "./shared/combineApiRequests" import { combineCommandSequences } from "./shared/combineCommandSequences" +import { findLastIndex } from "./utils" const SYSTEM_PROMPT = () => `You are Claude Dev, a highly skilled software developer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices. @@ -452,7 +453,8 @@ export class ClaudeDev { private async resumeTaskFromHistory() { const modifiedClaudeMessages = await this.getSavedClaudeMessages() - // need to modify claude messages for good ux, i.e. if the last message is an api_request_started, then remove it otherwise the user will think the request is still loading + + // Need to modify claude messages for good ux, i.e. if the last message is an api_request_started, then remove it otherwise the user will think the request is still loading const lastApiReqStartedIndex = modifiedClaudeMessages.reduce( (lastIndex, m, index) => (m.type === "say" && m.say === "api_req_started" ? index : lastIndex), -1 @@ -464,6 +466,16 @@ export class ClaudeDev { if (lastApiReqStartedIndex > lastApiReqFinishedIndex && lastApiReqStartedIndex !== -1) { modifiedClaudeMessages.splice(lastApiReqStartedIndex, 1) } + + // Remove any resume messages that may have been added before + const lastRelevantMessageIndex = findLastIndex( + modifiedClaudeMessages, + (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task") + ) + if (lastRelevantMessageIndex !== -1) { + modifiedClaudeMessages.splice(lastRelevantMessageIndex + 1) + } + await this.overwriteClaudeMessages(modifiedClaudeMessages) this.claudeMessages = await this.getSavedClaudeMessages() diff --git a/src/utils/array-helpers.ts b/src/utils/array-helpers.ts new file mode 100644 index 0000000..c20e327 --- /dev/null +++ b/src/utils/array-helpers.ts @@ -0,0 +1,17 @@ +/** + * Returns the index of the last element in the array where predicate is true, and -1 + * otherwise. + * @param array The source array to search in + * @param predicate find calls predicate once for each element of the array, in descending + * order, until it finds one where predicate returns true. If such an element is found, + * findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1. + */ +export function findLastIndex(array: Array, predicate: (value: T, index: number, obj: T[]) => boolean): number { + let l = array.length + while (l--) { + if (predicate(array[l], l, array)) { + return l + } + } + return -1 +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 50e1c47..fe0548d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,3 +2,4 @@ export * from "./getNonce" export * from "./getUri" export * from "./process-images" export * from "./export-markdown" +export * from "./array-helpers"