Skip to content

Commit 29e1c39

Browse files
author
Marian Popeanga
committed
Feature #2112 Rename tasks in history
1 parent 6c76f90 commit 29e1c39

40 files changed

Lines changed: 536 additions & 16 deletions

‎packages/types/src/__tests__/history.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ describe("pendingTaskActionSchema", () => {
4444
expect(parsed.pendingAction?.actionId).toBe("finish-1")
4545
})
4646
})
47+
48+
describe("historyItemSchema", () => {
49+
const baseItem = {
50+
id: "task-1",
51+
number: 1,
52+
ts: 1,
53+
task: "Original prompt",
54+
tokensIn: 0,
55+
tokensOut: 0,
56+
totalCost: 0,
57+
}
58+
59+
it("keeps a user-assigned title", () => {
60+
expect(historyItemSchema.parse({ ...baseItem, title: "Renamed" }).title).toBe("Renamed")
61+
})
62+
63+
it("leaves title unset when absent", () => {
64+
expect(historyItemSchema.parse(baseItem)).not.toHaveProperty("title")
65+
})
66+
})

‎packages/types/src/history.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const historyItemSchema = z.object({
3333
number: z.number(),
3434
ts: z.number(),
3535
task: z.string(),
36+
title: z.string().optional(), // User-assigned display name; `task` keeps the original prompt
3637
tokensIn: z.number(),
3738
tokensOut: z.number(),
3839
cacheWrites: z.number().optional(),

‎packages/types/src/vscode-extension-host.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ export interface WebviewMessage {
502502
| "deleteTaskWithId"
503503
| "abandonSubtaskWithId"
504504
| "exportTaskWithId"
505+
| "renameTaskWithId"
505506
| "importSettings"
506507
| "exportSettings"
507508
| "resetState"

‎src/core/webview/ClineProvider.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,21 @@ export class ClineProvider
24502450
}
24512451
}
24522452

2453+
/*
2454+
* Sets a user-assigned display title for a history item. A blank title
2455+
* clears it so the history falls back to the original prompt.
2456+
*/
2457+
async renameTaskWithId(id: string, title: string | undefined) {
2458+
const historyItem = this.taskHistoryStore.get(id)
2459+
if (!historyItem) {
2460+
this.log(`[renameTaskWithId] task ${id} not found in history`)
2461+
return
2462+
}
2463+
2464+
const trimmed = title?.trim()
2465+
await this.updateTaskHistory({ ...historyItem, title: trimmed ? trimmed : undefined })
2466+
}
2467+
24532468
/* Condenses a task's message history to use fewer tokens. */
24542469
async condenseTaskContext(taskId: string) {
24552470
const task = this.taskRegistry.getById(taskId)

‎src/core/webview/__tests__/ClineProvider.taskHistory.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,56 @@ describe("ClineProvider Task History Synchronization", () => {
597597
})
598598
})
599599

600+
describe("renameTaskWithId", () => {
601+
const getStored = (id: string) => provider.taskHistoryStore.get(id)
602+
603+
it("stores a trimmed title and broadcasts the updated item", async () => {
604+
await provider.resolveWebviewView(mockWebviewView)
605+
provider.isViewLaunched = true
606+
await provider.updateTaskHistory(createHistoryItem({ id: "task-rename", task: "Original prompt" }))
607+
mockPostMessage.mockClear()
608+
609+
await provider.renameTaskWithId("task-rename", " Renamed ")
610+
611+
expect(getStored("task-rename")).toMatchObject({ title: "Renamed", task: "Original prompt" })
612+
const updates = findCallsByType(mockPostMessage.mock.calls, "taskHistoryItemUpdated")
613+
expect(updates.at(-1)?.[0].taskHistoryItem).toMatchObject({ id: "task-rename", title: "Renamed" })
614+
})
615+
616+
it.each([
617+
["an empty string", ""],
618+
["whitespace", " "],
619+
["undefined", undefined],
620+
])("clears the title when given %s", async (_label, title) => {
621+
await provider.updateTaskHistory(
622+
createHistoryItem({ id: "task-clear", task: "Original prompt", title: "Old title" }),
623+
{ broadcast: false },
624+
)
625+
626+
await provider.renameTaskWithId("task-clear", title)
627+
628+
expect(getStored("task-clear")?.title).toBeUndefined()
629+
expect(getStored("task-clear")?.task).toBe("Original prompt")
630+
})
631+
632+
it("keeps the title when the task later saves metadata without one", async () => {
633+
const item = createHistoryItem({ id: "task-keep", task: "Original prompt", status: "active" })
634+
await provider.updateTaskHistory(item, { broadcast: false })
635+
await provider.renameTaskWithId("task-keep", "Renamed")
636+
637+
// Mirrors Task.saveClineMessages(), whose taskMetadata() result never carries a title.
638+
await provider.updateTaskHistory({ ...item, tokensIn: 500 }, { broadcast: false })
639+
640+
expect(getStored("task-keep")).toMatchObject({ title: "Renamed", tokensIn: 500 })
641+
})
642+
643+
it("does nothing for an unknown task", async () => {
644+
await provider.renameTaskWithId("missing-task", "Renamed")
645+
646+
expect(getStored("missing-task")).toBeUndefined()
647+
})
648+
})
649+
600650
describe("broadcastTaskHistoryUpdate", () => {
601651
it("sends taskHistoryUpdated message with sorted history", async () => {
602652
await provider.resolveWebviewView(mockWebviewView)

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,11 @@ export const webviewMessageHandler = async (
981981
case "exportTaskWithId":
982982
await provider.exportTaskWithId(message.text!)
983983
break
984+
case "renameTaskWithId":
985+
if (message.taskId) {
986+
await provider.renameTaskWithId(message.taskId, message.text)
987+
}
988+
break
984989
case "getTaskWithAggregatedCosts": {
985990
try {
986991
const taskId = message.text

‎webview-ui/src/components/chat/TaskActions.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,39 @@ import { useCopyToClipboard } from "@/utils/clipboard"
88
import { useExtensionState } from "@/context/ExtensionStateContext"
99

1010
import { DeleteTaskDialog } from "../history/DeleteTaskDialog"
11-
import { CopyIcon, CheckIcon, DownloadIcon, Trash2Icon, FileJsonIcon, MessageSquareCodeIcon } from "lucide-react"
11+
import {
12+
CopyIcon,
13+
CheckIcon,
14+
DownloadIcon,
15+
Trash2Icon,
16+
FileJsonIcon,
17+
MessageSquareCodeIcon,
18+
PencilIcon,
19+
} from "lucide-react"
1220
import { LucideIconButton } from "./LucideIconButton"
1321

1422
interface TaskActionsProps {
1523
item?: HistoryItem
1624
buttonsDisabled: boolean
25+
onRename?: () => void
1726
}
1827

19-
export const TaskActions = ({ item, buttonsDisabled }: TaskActionsProps) => {
28+
export const TaskActions = ({ item, buttonsDisabled, onRename }: TaskActionsProps) => {
2029
const [deleteTaskId, setDeleteTaskId] = useState<string | null>(null)
2130
const { t } = useTranslation()
2231
const { copyWithFeedback, showCopyFeedback } = useCopyToClipboard()
2332
const { debug } = useExtensionState()
2433

2534
return (
2635
<div className="flex flex-row items-center -ml-0.5 mt-1 gap-1">
36+
{item?.id && onRename && (
37+
<LucideIconButton
38+
icon={PencilIcon}
39+
title={t("history:renameTask")}
40+
data-testid="rename-current-task-button"
41+
onClick={onRename}
42+
/>
43+
)}
2744
<LucideIconButton
2845
icon={DownloadIcon}
2946
title={t("chat:task.export")}

‎webview-ui/src/components/chat/TaskHeader.tsx‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useRef, useState, useMemo } from "react"
1+
import { memo, useCallback, useRef, useState, useMemo } from "react"
22
import { useTranslation } from "react-i18next"
33
import {
44
ChevronUp,
@@ -23,6 +23,7 @@ import { useSelectedModel } from "@/components/ui/hooks/useSelectedModel"
2323
import { vscode } from "@src/utils/vscode"
2424

2525
import Thumbnails from "../common/Thumbnails"
26+
import { TaskTitleEditor } from "../history/TaskTitleEditor"
2627
import { TaskActions } from "./TaskActions"
2728
import { ContextWindowProgress } from "./ContextWindowProgress"
2829
import { Mention } from "./Mention"
@@ -66,6 +67,10 @@ const TaskHeader = ({
6667
const { apiConfiguration, currentTaskItem } = useExtensionState()
6768
const { id: modelId, info: model } = useSelectedModel(apiConfiguration)
6869
const [isTaskExpanded, setIsTaskExpanded] = useState(false)
70+
const [isRenaming, setIsRenaming] = useState(false)
71+
const startRename = useCallback(() => setIsRenaming(true), [])
72+
const stopRename = useCallback(() => setIsRenaming(false), [])
73+
const customTitle = currentTaskItem?.title
6974

7075
const textContainerRef = useRef<HTMLDivElement>(null)
7176
const textRef = useRef<HTMLDivElement>(null)
@@ -179,10 +184,19 @@ const TaskHeader = ({
179184
<div className="flex justify-between items-center gap-0">
180185
<div className="flex items-center select-none grow min-w-0">
181186
<div className="grow min-w-0">
182-
{isTaskExpanded && <span className="font-bold">{t("chat:task.title")}</span>}
187+
{isTaskExpanded &&
188+
(isRenaming && currentTaskItem ? (
189+
<TaskTitleEditor item={currentTaskItem} onDone={stopRename} className="text-sm" />
190+
) : (
191+
<span className="font-bold">{customTitle || t("chat:task.title")}</span>
192+
))}
183193
{!isTaskExpanded && (
184194
<div className="flex items-center gap-2 whitespace-nowrap overflow-hidden text-ellipsis">
185-
<Mention text={task.text} />
195+
{customTitle ? (
196+
<span className="overflow-hidden text-ellipsis">{customTitle}</span>
197+
) : (
198+
<Mention text={task.text} />
199+
)}
186200
</div>
187201
)}
188202
</div>
@@ -336,7 +350,11 @@ const TaskHeader = ({
336350
{task.images && task.images.length > 0 && <Thumbnails images={task.images} />}
337351

338352
<div onClick={(e) => e.stopPropagation()}>
339-
<TaskActions item={currentTaskItem} buttonsDisabled={buttonsDisabled} />
353+
<TaskActions
354+
item={currentTaskItem}
355+
buttonsDisabled={buttonsDisabled}
356+
onRename={isRenaming ? undefined : startRename}
357+
/>
340358
</div>
341359

342360
<div className="pt-3 mt-2 -mx-2.5 px-2.5 border-t border-vscode-sideBar-background">

‎webview-ui/src/components/chat/__tests__/TaskActions.spec.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ describe("TaskActions", () => {
8383
expect(screen.queryByTestId("share-button")).not.toBeInTheDocument()
8484
})
8585

86+
it("calls onRename when the rename button is clicked", () => {
87+
const onRename = vi.fn()
88+
render(<TaskActions item={mockItem} buttonsDisabled={false} onRename={onRename} />)
89+
90+
fireEvent.click(screen.getByTestId("rename-current-task-button"))
91+
92+
expect(onRename).toHaveBeenCalledTimes(1)
93+
})
94+
95+
it("does not render the rename button without onRename", () => {
96+
render(<TaskActions item={mockItem} buttonsDisabled={false} />)
97+
98+
expect(screen.queryByTestId("rename-current-task-button")).not.toBeInTheDocument()
99+
})
100+
86101
it("renders export button", () => {
87102
render(<TaskActions item={mockItem} buttonsDisabled={false} />)
88103

‎webview-ui/src/components/chat/__tests__/TaskHeader.spec.tsx‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ vi.mock("@vscode/webview-ui-toolkit/react", () => ({
3838
// Create a variable to hold the mock state
3939
const mockExtensionState: {
4040
apiConfiguration: ProviderSettings
41-
currentTaskItem: { id: string } | null
41+
currentTaskItem: { id: string; task?: string; title?: string } | null
4242
clineMessages: any[]
4343
taskHistory: any[]
4444
} = {
@@ -272,6 +272,44 @@ describe("TaskHeader", () => {
272272
})
273273
})
274274

275+
describe("Rename", () => {
276+
beforeEach(() => {
277+
mockPostMessage.mockClear()
278+
mockExtensionState.currentTaskItem = { id: "test-task-id", task: "Test task" }
279+
})
280+
281+
afterEach(() => {
282+
mockExtensionState.currentTaskItem = { id: "test-task-id" }
283+
})
284+
285+
it("shows the custom title instead of the first message when the task is renamed", () => {
286+
mockExtensionState.currentTaskItem = { id: "test-task-id", task: "Test task", title: "My title" }
287+
renderTaskHeader()
288+
289+
expect(screen.getByText("My title")).toBeInTheDocument()
290+
expect(screen.queryByText("Test task")).not.toBeInTheDocument()
291+
})
292+
293+
it("renames from the expanded header without collapsing it", () => {
294+
renderTaskHeader()
295+
fireEvent.click(screen.getByText("Test task"))
296+
297+
fireEvent.click(screen.getByTestId("rename-current-task-button"))
298+
const input = screen.getByTestId("task-title-input")
299+
fireEvent.click(input)
300+
fireEvent.change(input, { target: { value: "Renamed" } })
301+
fireEvent.keyDown(input, { key: "Enter" })
302+
303+
expect(mockPostMessage).toHaveBeenCalledWith({
304+
type: "renameTaskWithId",
305+
taskId: "test-task-id",
306+
text: "Renamed",
307+
})
308+
expect(screen.queryByTestId("task-title-input")).not.toBeInTheDocument()
309+
expect(screen.getByTestId("rename-current-task-button")).toBeInTheDocument()
310+
})
311+
})
312+
275313
describe("Context window percentage calculation", () => {
276314
// The percentage should be calculated as:
277315
// contextTokens / (contextWindow - reservedForOutput) * 100

0 commit comments

Comments
 (0)