Skip to content

Commit 54ee431

Browse files
committed
feat: add completion change actions
1 parent 6daa153 commit 54ee431

29 files changed

Lines changed: 605 additions & 6 deletions

‎packages/types/src/message.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,76 @@ export const clineMessageSchema = z.object({
275275

276276
export type ClineMessage = z.infer<typeof clineMessageSchema>
277277

278+
export interface CompletionCheckpoint {
279+
ts: number
280+
commitHash: string
281+
}
282+
283+
const isInitialTaskMessage = (message: ClineMessage | undefined): boolean => {
284+
return message?.type === "say" && message.say === "text"
285+
}
286+
287+
const isUserFeedbackMessage = (message: ClineMessage): boolean => {
288+
return message.type === "say" && message.say === "user_feedback"
289+
}
290+
291+
const isCompletionMessage = (message: ClineMessage): boolean => {
292+
return (
293+
(message.type === "ask" && message.ask === "completion_result") ||
294+
(message.type === "say" && message.say === "completion_result")
295+
)
296+
}
297+
298+
const isCheckpointMessage = (message: ClineMessage): boolean => {
299+
return message.type === "say" && message.say === "checkpoint_saved" && typeof message.text === "string"
300+
}
301+
302+
function findLastIndexBefore(
303+
messages: ClineMessage[],
304+
beforeIndex: number,
305+
predicate: (message: ClineMessage) => boolean,
306+
): number {
307+
for (let i = beforeIndex - 1; i >= 0; i--) {
308+
const message = messages[i]
309+
310+
if (message && predicate(message)) {
311+
return i
312+
}
313+
}
314+
315+
return -1
316+
}
317+
318+
/**
319+
* Finds the checkpoint that should anchor completion-result actions.
320+
*
321+
* The baseline is the first checkpoint created after the latest user prompt in
322+
* the turn that produced the completion. Restoring to that checkpoint reverts
323+
* changes made for the latest prompt, and diffing from it shows the same scoped
324+
* changes.
325+
*/
326+
export function getCompletionCheckpoint(messages: ClineMessage[]): CompletionCheckpoint | undefined {
327+
const completionIndex = findLastIndexBefore(messages, messages.length, isCompletionMessage)
328+
const searchEnd = completionIndex === -1 ? messages.length : completionIndex
329+
const latestUserFeedbackIndex = findLastIndexBefore(messages, searchEnd, isUserFeedbackMessage)
330+
const latestUserPromptIndex =
331+
latestUserFeedbackIndex !== -1 ? latestUserFeedbackIndex : isInitialTaskMessage(messages[0]) ? 0 : -1
332+
333+
if (latestUserPromptIndex === -1) {
334+
return undefined
335+
}
336+
337+
for (let i = latestUserPromptIndex + 1; i < searchEnd; i++) {
338+
const message = messages[i]
339+
340+
if (message && isCheckpointMessage(message)) {
341+
return { ts: message.ts, commitHash: message.text! }
342+
}
343+
}
344+
345+
return undefined
346+
}
347+
278348
/**
279349
* TokenUsage
280350
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ export interface WebviewMessage {
504504
| "openCustomModesSettings"
505505
| "checkpointDiff"
506506
| "checkpointRestore"
507+
| "completionCheckpointDiff"
508+
| "completionCheckpointRestore"
507509
| "deleteMcpServer"
508510
| "codebaseIndexEnabled"
509511
| "telemetrySetting"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { getCompletionCheckpoint, type ClineMessage } from "@roo-code/types"
2+
3+
describe("getCompletionCheckpoint", () => {
4+
it("returns the first checkpoint after the latest user prompt before completion", () => {
5+
const messages: ClineMessage[] = [
6+
{ type: "say", say: "text", ts: 1, text: "Initial task" },
7+
{ type: "say", say: "checkpoint_saved", ts: 2, text: "initial-checkpoint" },
8+
{ type: "say", say: "completion_result", ts: 3, text: "First completion" },
9+
{ type: "say", say: "user_feedback", ts: 4, text: "Change it" },
10+
{ type: "say", say: "checkpoint_saved", ts: 5, text: "latest-prompt-checkpoint" },
11+
{ type: "say", say: "checkpoint_saved", ts: 6, text: "later-edit-checkpoint" },
12+
{ type: "ask", ask: "completion_result", ts: 7, text: "", partial: false },
13+
]
14+
15+
expect(getCompletionCheckpoint(messages)).toEqual({
16+
ts: 5,
17+
commitHash: "latest-prompt-checkpoint",
18+
})
19+
})
20+
21+
it("returns undefined when completion has no checkpoint after the latest user prompt", () => {
22+
const messages: ClineMessage[] = [
23+
{ type: "say", say: "text", ts: 1, text: "Initial task" },
24+
{ type: "say", say: "checkpoint_saved", ts: 2, text: "initial-checkpoint" },
25+
{ type: "say", say: "user_feedback", ts: 3, text: "Change it" },
26+
{ type: "ask", ask: "completion_result", ts: 4, text: "", partial: false },
27+
]
28+
29+
expect(getCompletionCheckpoint(messages)).toBeUndefined()
30+
})
31+
})

‎src/core/webview/__tests__/webviewMessageHandler.checkpoint.spec.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
2626
// Setup mock Cline instance
2727
mockCline = {
2828
taskId: "test-task-123",
29+
isInitialized: true,
2930
clineMessages: [
3031
{ ts: 1, type: "user", say: "user", text: "First message" },
3132
{ ts: 2, type: "assistant", say: "checkpoint_saved", text: "abc123" },
@@ -37,6 +38,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
3738
{ ts: 3, role: "user", content: [{ type: "text", text: "Message to delete" }] },
3839
{ ts: 4, role: "assistant", content: [{ type: "text", text: "After message" }] },
3940
],
41+
checkpointDiff: vi.fn(),
4042
checkpointRestore: vi.fn(),
4143
overwriteClineMessages: vi.fn(),
4244
overwriteApiConversationHistory: vi.fn(),
@@ -52,6 +54,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
5254
})),
5355
createTaskWithHistoryItem: vi.fn(),
5456
setPendingEditOperation: vi.fn(),
57+
cancelTask: vi.fn(),
5558
contextProxy: {
5659
globalStorageUri: { fsPath: "/test/storage" },
5760
},
@@ -134,4 +137,53 @@ describe("webviewMessageHandler - checkpoint operations", () => {
134137
})
135138
})
136139
})
140+
141+
describe("completion checkpoint actions", () => {
142+
beforeEach(() => {
143+
mockCline.clineMessages = [
144+
{ ts: 1, type: "say", say: "text", text: "Initial task" },
145+
{ ts: 2, type: "say", say: "checkpoint_saved", text: "initial-checkpoint" },
146+
{ ts: 3, type: "say", say: "user_feedback", text: "Latest prompt" },
147+
{ ts: 4, type: "say", say: "checkpoint_saved", text: "latest-prompt-checkpoint" },
148+
{ ts: 5, type: "say", say: "completion_result", text: "Task complete" },
149+
{ ts: 6, type: "ask", ask: "completion_result", text: "", partial: false },
150+
]
151+
})
152+
153+
it("diffs changes from the checkpoint created after the latest prompt", async () => {
154+
await webviewMessageHandler(mockProvider, { type: "completionCheckpointDiff" })
155+
156+
expect(mockCline.checkpointDiff).toHaveBeenCalledWith({
157+
ts: 4,
158+
commitHash: "latest-prompt-checkpoint",
159+
mode: "to-current",
160+
})
161+
})
162+
163+
it("restores files and task state to the checkpoint created after the latest prompt", async () => {
164+
await webviewMessageHandler(mockProvider, { type: "completionCheckpointRestore" })
165+
166+
expect(mockProvider.cancelTask).toHaveBeenCalled()
167+
expect(mockCline.checkpointRestore).toHaveBeenCalledWith({
168+
ts: 4,
169+
commitHash: "latest-prompt-checkpoint",
170+
mode: "restore",
171+
})
172+
})
173+
174+
it("does not diff or restore when no latest-prompt checkpoint exists", async () => {
175+
mockCline.clineMessages = [
176+
{ ts: 1, type: "say", say: "text", text: "Initial task" },
177+
{ ts: 2, type: "say", say: "user_feedback", text: "Latest prompt" },
178+
{ ts: 3, type: "ask", ask: "completion_result", text: "", partial: false },
179+
]
180+
181+
await webviewMessageHandler(mockProvider, { type: "completionCheckpointDiff" })
182+
await webviewMessageHandler(mockProvider, { type: "completionCheckpointRestore" })
183+
184+
expect(mockCline.checkpointDiff).not.toHaveBeenCalled()
185+
expect(mockCline.checkpointRestore).not.toHaveBeenCalled()
186+
expect(mockProvider.cancelTask).not.toHaveBeenCalled()
187+
})
188+
})
137189
})

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ExperimentId,
2222
checkoutDiffPayloadSchema,
2323
checkoutRestorePayloadSchema,
24+
getCompletionCheckpoint,
2425
} from "@roo-code/types"
2526
import { customToolRegistry } from "@roo-code/core"
2627
import { CloudService } from "@roo-code/cloud"
@@ -1288,6 +1289,46 @@ export const webviewMessageHandler = async (
12881289

12891290
break
12901291
}
1292+
case "completionCheckpointDiff": {
1293+
const currentCline = provider.getCurrentTask()
1294+
const checkpoint = currentCline ? getCompletionCheckpoint(currentCline.clineMessages) : undefined
1295+
1296+
if (currentCline && checkpoint) {
1297+
await currentCline.checkpointDiff({
1298+
ts: checkpoint.ts,
1299+
commitHash: checkpoint.commitHash,
1300+
mode: "to-current",
1301+
})
1302+
}
1303+
1304+
break
1305+
}
1306+
case "completionCheckpointRestore": {
1307+
const currentCline = provider.getCurrentTask()
1308+
const checkpoint = currentCline ? getCompletionCheckpoint(currentCline.clineMessages) : undefined
1309+
1310+
if (checkpoint) {
1311+
await provider.cancelTask()
1312+
1313+
try {
1314+
await pWaitFor(() => provider.getCurrentTask()?.isInitialized === true, { timeout: 3_000 })
1315+
} catch (error) {
1316+
vscode.window.showErrorMessage(t("common:errors.checkpoint_timeout"))
1317+
}
1318+
1319+
try {
1320+
await provider.getCurrentTask()?.checkpointRestore({
1321+
ts: checkpoint.ts,
1322+
commitHash: checkpoint.commitHash,
1323+
mode: "restore",
1324+
})
1325+
} catch (error) {
1326+
vscode.window.showErrorMessage(t("common:errors.checkpoint_failed"))
1327+
}
1328+
}
1329+
1330+
break
1331+
}
12911332
case "cancelTask":
12921333
await provider.cancelTask()
12931334
break

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
ClineApiReqInfo,
1212
ClineAskUseMcpServer,
1313
ClineSayTool,
14+
CompletionCheckpoint,
1415
} from "@roo-code/types"
1516

1617
import { Mode } from "@roo/modes"
@@ -75,6 +76,7 @@ import {
7576
import { cn } from "@/lib/utils"
7677
import { PathTooltip } from "../ui/PathTooltip"
7778
import { OpenMarkdownPreviewButton } from "./OpenMarkdownPreviewButton"
79+
import { SeeNewChangesButtons } from "./SeeNewChangesButtons"
7880

7981
// Helper function to get previous todos before a specific message
8082
function getPreviousTodos(messages: ClineMessage[], currentMessageTs: number): any[] {
@@ -124,6 +126,7 @@ interface ChatRowProps {
124126
isFollowUpAutoApprovalPaused?: boolean
125127
editable?: boolean
126128
hasCheckpoint?: boolean
129+
completionCheckpoint?: CompletionCheckpoint
127130
onJumpToPreviousCheckpoint?: () => void
128131
}
129132

@@ -178,12 +181,21 @@ export const ChatRowContent = ({
178181
onBatchFileResponse,
179182
isFollowUpAnswered,
180183
isFollowUpAutoApprovalPaused,
184+
completionCheckpoint,
181185
onJumpToPreviousCheckpoint,
182186
}: ChatRowContentProps) => {
183187
const { t, i18n } = useTranslation()
184188

185-
const { mcpServers, alwaysAllowMcp, currentCheckpoint, mode, apiConfiguration, clineMessages, currentTaskItem } =
186-
useExtensionState()
189+
const {
190+
mcpServers,
191+
alwaysAllowMcp,
192+
currentCheckpoint,
193+
mode,
194+
apiConfiguration,
195+
clineMessages,
196+
currentTaskItem,
197+
enableCheckpoints,
198+
} = useExtensionState()
187199
const { info: model } = useSelectedModel(apiConfiguration)
188200
const [isEditing, setIsEditing] = useState(false)
189201
const [editedContent, setEditedContent] = useState("")
@@ -1331,6 +1343,9 @@ export const ChatRowContent = ({
13311343
</div>
13321344
<div className="border-l border-green-600/30 ml-2 pl-4 pb-1">
13331345
<Markdown markdown={message.text} />
1346+
{!message.partial && enableCheckpoints !== false && completionCheckpoint ? (
1347+
<SeeNewChangesButtons checkpoint={completionCheckpoint} />
1348+
) : null}
13341349
</div>
13351350
</div>
13361351
)

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getCostBreakdownIfNeeded } from "@src/utils/costFormatting"
1111
import { batchConsecutive } from "@src/utils/batchConsecutive"
1212

1313
import type { ClineAsk, ClineSayTool, ClineMessage, ExtensionMessage, AudioType, SuggestionItem } from "@roo-code/types"
14-
import { getSuggestionMode, isRetiredProvider } from "@roo-code/types"
14+
import { getCompletionCheckpoint, getSuggestionMode, isRetiredProvider } from "@roo-code/types"
1515

1616
import { findLast } from "@roo/array"
1717
import { combineApiRequests } from "@roo/combineApiRequests"
@@ -129,6 +129,18 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
129129
}, [messages, currentTaskTodos])
130130

131131
const modifiedMessages = useMemo(() => combineApiRequests(combineCommandSequences(messages.slice(1))), [messages])
132+
const completionCheckpoint = useMemo(() => getCompletionCheckpoint(messages), [messages])
133+
const completionResultTs = useMemo(() => {
134+
for (let i = messages.length - 1; i >= 0; i--) {
135+
const message = messages[i]
136+
137+
if (message?.type === "say" && message.say === "completion_result") {
138+
return message.ts
139+
}
140+
}
141+
142+
return undefined
143+
}, [messages])
132144

133145
// Has to be after api_req_finished are all reduced into api_req_started messages.
134146
const apiMetrics = useMemo(() => getApiMetrics(modifiedMessages), [modifiedMessages])
@@ -374,6 +386,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
374386
break
375387
case "completion_result":
376388
// Extension waiting for feedback, but we can just present a new task button.
389+
// Kilo-style change inspection/restoration buttons are rendered inline on the completion row.
377390
// Only play celebration sound if there are no queued messages.
378391
if (!isPartial && messageQueue.length === 0) {
379392
playSound("celebration")
@@ -788,6 +801,8 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
788801
}
789802
break
790803
case "completion_result":
804+
startNewTask()
805+
break
791806
case "resume_completed_task":
792807
// Waiting for feedback, but we can just present a new task button
793808
startNewTask()
@@ -1476,6 +1491,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
14761491
})()
14771492
}
14781493
hasCheckpoint={hasCheckpoint}
1494+
completionCheckpoint={messageOrGroup.ts === completionResultTs ? completionCheckpoint : undefined}
14791495
onJumpToPreviousCheckpoint={handleScrollToLatestCheckpoint}
14801496
/>
14811497
)
@@ -1485,6 +1501,8 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
14851501
toggleRowExpansion,
14861502
modifiedMessages,
14871503
groupedMessages.length,
1504+
completionCheckpoint,
1505+
completionResultTs,
14881506
handleRowHeightChange,
14891507
isStreaming,
14901508
handleSuggestionClickInRow,

0 commit comments

Comments
 (0)