This commit is contained in:
shengsuan-cloud committed 2025-12-30 13:45:34 +08:00
commit f6032d9c87
676 files changed
+59829 -15880

No files matched your search

+251
View File
@@ -0,0 +1,251 @@
import CheckpointTracker from "@integrations/checkpoints/CheckpointTracker"
import { findLast } from "@shared/array"
import { Empty } from "@shared/proto/cline/common"
import { ExplainChangesRequest } from "@shared/proto/cline/task"
import { HostProvider } from "@/hosts/host-provider"
import { ShowMessageType } from "@/shared/proto/index.host"
import { Controller } from ".."
import { sendRelinquishControlEvent } from "../ui/subscribeToRelinquishControl"
import {
buildDiffContent,
openDiffView,
setupCommentController,
streamAIExplanationComments,
stringifyConversationHistory,
} from "./explainChangesShared"
/**
* Explains the changes made by the AI and adds inline comments explaining them.
*
* This handler streams comments in real-time:
* 1. Gets the diff from the checkpoint tracker
* 2. Opens the diff view IMMEDIATELY so user sees progress
* 3. Streams the AI response and adds comments as they're generated
* 4. Each comment appears in the diff view as soon as it's parsed
*/
export async function explainChanges(controller: Controller, request: ExplainChangesRequest): Promise<Empty> {
const relinquishButton = () => {
sendRelinquishControlEvent()
}
try {
// Validate we have an active task with checkpoint manager
if (!controller.task) {
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "No active task",
})
relinquishButton()
return Empty.create({})
}
const checkpointManager = controller.task.checkpointManager as any
if (!checkpointManager) {
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Checkpoints not enabled",
})
relinquishButton()
return Empty.create({})
}
// Check if checkpoints are enabled
if (!checkpointManager.config?.enableCheckpoints) {
HostProvider.window.showMessage({
type: ShowMessageType.INFORMATION,
message: "Checkpoints are disabled in settings. Cannot review changes.",
})
relinquishButton()
return Empty.create({})
}
// Get message state handler
const messageStateHandler = checkpointManager.services?.messageStateHandler
if (!messageStateHandler) {
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Message state handler not available",
})
relinquishButton()
return Empty.create({})
}
// Find the message
const clineMessages = messageStateHandler.getClineMessages()
const messageIndex = clineMessages.findIndex((m: any) => m.ts === request.messageTs)
const message = clineMessages[messageIndex]
if (!message) {
console.error(`[explainChanges] Message not found for timestamp ${request.messageTs}`)
relinquishButton()
return Empty.create({})
}
const hash = message.lastCheckpointHash
if (!hash) {
console.error(`[explainChanges] No checkpoint hash found for message ${request.messageTs}`)
relinquishButton()
return Empty.create({})
}
// Initialize checkpoint tracker if needed (same logic as presentMultifileDiff)
if (
!checkpointManager.state?.checkpointTracker &&
checkpointManager.config?.enableCheckpoints &&
!checkpointManager.state?.checkpointManagerErrorMessage
) {
try {
const workspacePath = await checkpointManager.getWorkspacePath()
checkpointManager.state.checkpointTracker = await CheckpointTracker.create(
checkpointManager.task.taskId,
checkpointManager.config.enableCheckpoints,
workspacePath,
)
messageStateHandler.setCheckpointTracker(checkpointManager.state.checkpointTracker)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error"
console.error(`[explainChanges] Failed to initialize checkpoint tracker:`, errorMessage)
checkpointManager.state.checkpointManagerErrorMessage = errorMessage
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: errorMessage,
})
relinquishButton()
return Empty.create({})
}
}
const checkpointTracker = checkpointManager.state?.checkpointTracker as CheckpointTracker | undefined
if (!checkpointTracker) {
console.error(`[explainChanges] Checkpoint tracker not available`)
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Checkpoint tracker not available",
})
relinquishButton()
return Empty.create({})
}
// Get changed files (using seeNewChangesSinceLastTaskCompletion logic)
const lastTaskCompletedMessageCheckpointHash = findLast(
clineMessages.slice(0, messageIndex),
(m: any) => m.say === "completion_result",
)?.lastCheckpointHash
const firstCheckpointMessageCheckpointHash = clineMessages.find(
(m: any) => m.say === "checkpoint_created",
)?.lastCheckpointHash
const previousCheckpointHash = lastTaskCompletedMessageCheckpointHash || firstCheckpointMessageCheckpointHash
if (!previousCheckpointHash) {
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Unexpected error: No checkpoint hash found",
})
relinquishButton()
return Empty.create({})
}
const changedFiles = await checkpointTracker.getDiffSet(previousCheckpointHash, hash)
if (!changedFiles?.length) {
HostProvider.window.showMessage({
type: ShowMessageType.INFORMATION,
message: "No changes found to review",
})
relinquishButton()
return Empty.create({})
}
// Get API configuration
const apiConfiguration = controller.stateManager.getApiConfiguration()
if (!apiConfiguration) {
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "API configuration not available",
})
relinquishButton()
return Empty.create({})
}
// Get conversation summary for context
const apiConversationHistory = messageStateHandler.getApiConversationHistory()
const conversationSummary = stringifyConversationHistory(apiConversationHistory)
// Set up the comment controller with reply handler
const commentController = await setupCommentController(apiConfiguration, changedFiles, conversationSummary)
// Build the diff content for the AI
const diffContent = buildDiffContent(changedFiles)
// For 3+ files, cycle through each file showing comments as they stream
// For 2 or fewer files, just open the multi-diff view directly
const shouldRevealComments = changedFiles.length >= 3
// If 2 or fewer files, open the diff view first so user sees it immediately
if (!shouldRevealComments) {
await openDiffView("Explain Changes", changedFiles)
}
// Capture reference to the task for abort checking
const task = controller.task
// Stream AI explanation comments and add them as they arrive
// Each comment will open its virtual doc and scroll to show the comment (if 3+ files)
await streamAIExplanationComments(
apiConfiguration,
diffContent,
conversationSummary,
changedFiles,
// onCommentStart: Create the comment UI immediately when we know the location
(filePath, startLine, endLine) => {
const matchingFile = changedFiles.find((f) => f.absolutePath === filePath)
commentController.startStreamingComment(
filePath,
startLine,
endLine,
matchingFile?.relativePath,
matchingFile?.after,
shouldRevealComments, // Only cycle through files if 3+ files
)
},
// onCommentChunk: Append text as it streams in
(chunk) => {
commentController.appendToStreamingComment(chunk)
},
// onCommentEnd: Finalize the comment
() => {
commentController.endStreamingComment()
},
// shouldAbort: Check if task was cancelled
() => task?.taskState?.abort === true,
)
// Check if we were aborted during streaming
if (task?.taskState?.abort) {
// Close diff views and clear comments when cancelled
commentController.clearAllComments()
await commentController.closeDiffViews()
relinquishButton()
return Empty.create({})
}
// After all comments are done, open the multi-diff view to show everything together (if 3+ files)
if (shouldRevealComments) {
await openDiffView("Explain Changes", changedFiles)
}
// Relinquish button after comments are done
relinquishButton()
return Empty.create({})
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error"
console.error("Error in explainChanges:", errorMessage)
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Failed to explain changes: " + errorMessage,
})
sendRelinquishControlEvent()
return Empty.create({})
}
}
@@ -0,0 +1,451 @@
import { buildApiHandler } from "@core/api"
import { HostProvider } from "@/hosts/host-provider"
import { formatContentBlockToMarkdown } from "@/integrations/misc/export-markdown"
import { ApiConfiguration } from "@/shared/api"
import { ClineStorageMessage } from "@/shared/messages/content"
export interface ChangedFile {
relativePath: string
absolutePath: string
before: string
after: string
}
const EXPLAINER_SYSTEM_PROMPT = `You are an AI coding assistant called Cline that will be explaining code changes to a developer. Your goal is to help the user understand what changed and why.
- Use a friendly, conversational tone as if pair programming
- When relevant, briefly explain technical concepts or patterns used
- Focus on helping the user learn and understand the codebase
- Highlight any important decisions, trade-offs, or things the user should be aware of
Remember: The user wants to understand the changes well enough to maintain, extend, or debug this code themselves.
`
/**
* Add line numbers to content (0-indexed for AI reference)
*/
function addLineNumbers(content: string): string {
if (!content) {
return content
}
const lines = content.split("\n")
return lines.map((line, index) => `${index}: ${line}`).join("\n")
}
/**
* Build a unified diff content string for the AI
* Includes 0-indexed line numbers so the AI can reference specific lines
*/
export function buildDiffContent(changedFiles: ChangedFile[]): string {
const parts: string[] = []
for (const file of changedFiles) {
parts.push(`\n=== File: ${file.absolutePath} ===\n`)
parts.push(`--- Before ---\n${file.before || "(new file)"}\n`)
parts.push(
`--- After (use these line numbers for comments) ---\n${file.after ? addLineNumbers(file.after) : "(deleted)"}\n`,
)
}
return parts.join("\n")
}
/**
* Open the multi-file diff view with the changed files
*/
export async function openDiffView(title: string, changedFiles: ChangedFile[]): Promise<void> {
await HostProvider.diff.openMultiFileDiff({
title,
diffs: changedFiles.map((file) => ({
filePath: file.absolutePath,
leftContent: file.before,
rightContent: file.after,
})),
})
}
/**
* Set up the comment controller and reply handler
*/
export async function setupCommentController(
apiConfiguration: ApiConfiguration,
changedFiles: ChangedFile[],
conversationContext: string,
) {
const commentController = HostProvider.get().createCommentReviewController()
commentController.clearAllComments()
// Ensure the Comments panel won't auto-open when we add comments
await commentController.ensureCommentsViewDisabled()
// Set up reply handler for conversations
commentController.setOnReplyCallback(async (filePath, startLine, endLine, replyText, existingComments, onChunk) => {
await handleCommentReply(
apiConfiguration,
filePath,
startLine,
endLine,
replyText,
existingComments,
changedFiles,
conversationContext,
onChunk,
)
})
return commentController
}
/**
* Stream AI explanation comments with real-time updates.
* Uses a structured format that allows creating comment UI immediately when location is known,
* then streaming the comment text as it arrives.
*
* @param shouldAbort - Optional callback that returns true if the operation should be aborted
*/
export async function streamAIExplanationComments(
apiConfiguration: ApiConfiguration,
diffContent: string,
contextDescription: string,
changedFiles: ChangedFile[],
onCommentStart: (filePath: string, startLine: number, endLine: number) => void,
onCommentChunk: (chunk: string) => void,
onCommentEnd: () => void,
shouldAbort?: () => boolean,
): Promise<number> {
// Disable thinking/reasoning for faster response
const configWithoutThinking: ApiConfiguration = {
...apiConfiguration,
actModeThinkingBudgetTokens: 0,
planModeThinkingBudgetTokens: 0,
}
const apiHandler = buildApiHandler(configWithoutThinking, "act")
const fileCount = changedFiles.length
const maxCommentsPerFile = fileCount > 3 ? 1 : 3
const systemPrompt = `${EXPLAINER_SYSTEM_PROMPT}
CRITICAL: Create comments for LOGICAL GROUPINGS of changes, not individual lines. Think in terms of:
- "Added a new function that does X" (spanning the entire function)
- "Refactored error handling in this section" (spanning all related changes)
- "Updated imports and dependencies" (one comment for all import changes)
OUTPUT FORMAT - Use this exact structure for each comment:
@@@ FILE: /absolute/path/to/file.ts
@@@ LINE: 45
Your explanation of what changed and why goes here. Can be multiple sentences.
@@@
@@@ FILE: /absolute/path/to/other.ts
@@@ LINE: 20
Another explanation here.
@@@
Rules:
1. Start each comment with @@@ FILE: followed by the absolute file path
2. Next line must be @@@ LINE: followed by a single line number (0-indexed from the "After" content)
- For ADDITIONS or MODIFICATIONS: Use the LAST LINE of the changed code block
- For DELETIONS: Use the FIRST LINE where the deletion occurred (the line number in "After" where content was removed)
- The diff view collapses unchanged lines, so comments must be on a line that's part of the diff to be visible
3. Then write your comment text (can span multiple lines). Use markdown formatting where appropriate.
4. End with @@@ on its own line
5. Each file MUST have at least one comment, MAX ${maxCommentsPerFile} comment${maxCommentsPerFile > 1 ? "s" : ""} per file - focus on the most significant changes
6. Explain important/non-obvious changes, not every little thing. Skip trivial changes - ignore whitespace, formatting, simple renames, obvious fixes.
`
const userMessage = `Explain these code changes:
## Context
${contextDescription}
## Files changed
${changedFiles.map((f) => `- ${f.absolutePath}`).join("\n")}
## Diff content
${diffContent}
Output your explanation comments now using the @@@ format:`
let commentCount = 0
let buffer = ""
let currentFile: string | null = null
let currentStartLine: number | null = null
let currentEndLine: number | null = null
let inComment = false
try {
for await (const chunk of apiHandler.createMessage(systemPrompt, [{ role: "user", content: userMessage }])) {
// Check if we should abort before processing each chunk
if (shouldAbort?.()) {
// If we're in the middle of a comment, end it cleanly
if (inComment) {
onCommentEnd()
}
return commentCount
}
if (chunk.type === "text") {
buffer += chunk.text
// Process buffer line by line, keeping incomplete lines
while (true) {
// Check abort before processing each line
if (shouldAbort?.()) {
if (inComment) {
onCommentEnd()
}
return commentCount
}
const newlineIndex = buffer.indexOf("\n")
if (newlineIndex === -1) {
break
}
const line = buffer.substring(0, newlineIndex)
buffer = buffer.substring(newlineIndex + 1)
const trimmedLine = line.trim()
// Check for FILE header
if (trimmedLine.startsWith("@@@ FILE:")) {
const filePath = trimmedLine.substring("@@@ FILE:".length).trim()
const matchingFile = changedFiles.find((f) => f.absolutePath === filePath || f.relativePath === filePath)
currentFile = matchingFile?.absolutePath || filePath
continue
}
// Check for LINE header (single line number)
if (trimmedLine.startsWith("@@@ LINE:")) {
const lineStr = trimmedLine.substring("@@@ LINE:".length).trim()
const lineNum = parseInt(lineStr, 10)
if (!Number.isNaN(lineNum) && currentFile) {
currentStartLine = lineNum
currentEndLine = lineNum
// Now we have location - create the comment UI immediately!
onCommentStart(currentFile, currentStartLine, currentEndLine)
inComment = true
commentCount++
}
continue
}
// Check for end marker
if (trimmedLine === "@@@") {
if (inComment) {
onCommentEnd()
inComment = false
currentFile = null
currentStartLine = null
currentEndLine = null
}
continue
}
// If we're in a comment, stream the text
if (inComment) {
onCommentChunk(line + "\n")
}
}
// Stream partial content in buffer for more responsive UI
// But don't stream if it might be a marker (starts with @)
if (inComment && buffer.length > 0 && !buffer.startsWith("@")) {
onCommentChunk(buffer)
buffer = "" // Clear buffer after streaming
}
}
}
// Handle any remaining content in buffer
if (buffer.trim()) {
const trimmedBuffer = buffer.trim()
if (trimmedBuffer === "@@@") {
if (inComment) {
onCommentEnd()
inComment = false
}
} else if (inComment && !trimmedBuffer.startsWith("@@@")) {
onCommentChunk(buffer)
onCommentEnd()
inComment = false
}
} else if (inComment) {
onCommentEnd()
}
return commentCount
} catch (error) {
console.error("Error streaming AI explanation comments:", error)
if (inComment) {
onCommentEnd()
}
return commentCount
}
}
/**
* Handle a reply to a comment thread with streaming
*/
async function handleCommentReply(
apiConfiguration: ApiConfiguration,
filePath: string,
startLine: number,
endLine: number,
replyText: string,
existingComments: string[],
changedFiles: ChangedFile[],
conversationContext: string,
onChunk: (chunk: string) => void,
): Promise<void> {
// Disable thinking/reasoning for faster response
const configWithoutThinking: ApiConfiguration = {
...apiConfiguration,
actModeThinkingBudgetTokens: 0,
planModeThinkingBudgetTokens: 0,
}
// Find the relevant file
const file = changedFiles.find((f) => f.absolutePath === filePath)
if (!file) {
onChunk("Error: Could not find the file context")
return
}
// Get the relevant code snippet
const afterLines = file.after.split("\n")
const codeSnippet = afterLines.slice(startLine, endLine + 1).join("\n")
const apiHandler = buildApiHandler(configWithoutThinking, "act")
const systemPrompt = `${EXPLAINER_SYSTEM_PROMPT}
The user is asking followup questions about code change explanations you provided.
Respond helpfully to the user's question about the code.
Use markdown formatting where appropriate.
If the user asks you to make changes, fix something, or do any work that requires modifying code, let them know they can click the "Add to Cline Chat" button (the arrow icon in the top-right of the comment box) to send this conversation to the main Cline agent, which can then make the requested changes.
`
const userMessage = `## Context
${conversationContext}
## Code Being Discussed
File: ${file.relativePath}
Lines ${startLine + 1}-${endLine + 1}:
\`\`\`
${codeSnippet}
\`\`\`
## Comment Thread
${existingComments.join("\n\n")}
## User's Question
${replyText}
Please respond to the user's question about this code.`
try {
for await (const chunk of apiHandler.createMessage(systemPrompt, [{ role: "user", content: userMessage }])) {
if (chunk.type === "text") {
onChunk(chunk.text)
}
}
} catch (error) {
console.error("Error getting reply:", error)
onChunk(`Error: ${error instanceof Error ? error.message : "Unknown error"}`)
}
}
/**
* Stringify conversation history into a readable summary for context
*/
export function stringifyConversationHistory(apiConversationHistory: ClineStorageMessage[]): string {
if (!apiConversationHistory || apiConversationHistory.length === 0) {
return "No prior conversation context available."
}
return apiConversationHistory
.map((message) => {
const role = message.role === "user" ? "**User:**" : "**Assistant:**"
const content = Array.isArray(message.content)
? message.content.map((block) => formatContentBlockToMarkdown(block)).join("\n")
: message.content
return `${role}\n\n${content}\n\n`
})
.join("---\n\n")
}
/**
* Binary file extensions to exclude from diff view
*/
const BINARY_EXTENSIONS = new Set([
// Images
".png",
".jpg",
".jpeg",
".gif",
".bmp",
".ico",
".webp",
".svg",
".tiff",
".tif",
// Audio
".mp3",
".wav",
".ogg",
".flac",
".aac",
".m4a",
// Video
".mp4",
".avi",
".mov",
".wmv",
".flv",
".webm",
".mkv",
// Archives
".zip",
".tar",
".gz",
".rar",
".7z",
".bz2",
// Documents
".pdf",
".doc",
".docx",
".xls",
".xlsx",
".ppt",
".pptx",
// Fonts
".ttf",
".otf",
".woff",
".woff2",
".eot",
// Executables/binaries
".exe",
".dll",
".so",
".dylib",
".bin",
".o",
".a",
// Other
".db",
".sqlite",
".sqlite3",
".lock",
".wasm",
])
/**
* Check if a file is binary based on its extension
*/
export function isBinaryFile(filePath: string): boolean {
const ext = filePath.substring(filePath.lastIndexOf(".")).toLowerCase()
return BINARY_EXTENSIONS.has(ext)
}
@@ -103,6 +103,7 @@ export async function getTaskHistory(controller: Controller, request: GetTaskHis
tokensOut: item.tokensOut || 0,
cacheWrites: item.cacheWrites || 0,
cacheReads: item.cacheReads || 0,
modelId: item.modelId || "",
}))
return TaskHistoryArray.create({
+12
View File
@@ -80,6 +80,18 @@ export async function newTask(controller: Controller, request: NewTaskRequest):
...(request.taskSettings?.actModeApiProvider !== undefined && {
actModeApiProvider: convertProtoToApiProvider(request.taskSettings.actModeApiProvider),
}),
...(request.taskSettings?.hooksEnabled !== undefined && {
hooksEnabled: (() => {
const isEnabled = !!request.taskSettings.hooksEnabled
// Platform validation: Only allow enabling hooks on macOS and Linux
if (isEnabled && process.platform === "win32") {
throw new Error("Hooks are not yet supported on Windows")
}
return isEnabled
})(),
}),
}).filter(([_, value]) => value !== undefined),
)