mirror of
https://github.com/cline/cline.git
synced 2026-09-14 11:29:25 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
242c670761 |
@@ -71,7 +71,8 @@ enum ClineSay {
|
||||
CLINEIGNORE_ERROR = 23;
|
||||
CHECKPOINT_CREATED = 24;
|
||||
LOAD_MCP_DOCUMENTATION = 25;
|
||||
INFO = 26;
|
||||
CONVERSATION_SUMMARY = 26;
|
||||
INFO = 27;
|
||||
}
|
||||
|
||||
// Enum for ClineSayTool tool types
|
||||
|
||||
@@ -8,6 +8,7 @@ import cloneDeep from "clone-deep"
|
||||
import { ClineApiReqInfo, ClineMessage } from "@shared/ExtensionMessage"
|
||||
import { ApiHandler } from "@api/index"
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import { SummarizationService } from "../summarization/SummarizationService"
|
||||
|
||||
enum EditType {
|
||||
UNDEFINED = 0,
|
||||
@@ -50,9 +51,11 @@ export class ContextManager {
|
||||
// example: { 1 => { [0, 0 => [[<timestamp>, "text", "[NOTE] Some previous conversation history with the user has been removed ..."], ...] }] }
|
||||
// the above example would be how we update the first assistant message to indicate we truncated text
|
||||
private contextHistoryUpdates: Map<number, [number, Map<number, ContextUpdate[]>]>
|
||||
private summarizationService: SummarizationService
|
||||
|
||||
constructor() {
|
||||
this.contextHistoryUpdates = new Map()
|
||||
this.summarizationService = new SummarizationService()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,7 +109,7 @@ export class ContextManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* primary entry point for getting up to date context & truncating when required
|
||||
* primary entry point for getting up to date context & summarizing when required
|
||||
*/
|
||||
async getNewContextMessagesAndMetadata(
|
||||
apiConversationHistory: Anthropic.Messages.MessageParam[],
|
||||
@@ -117,8 +120,18 @@ export class ContextManager {
|
||||
taskDirectory: string,
|
||||
) {
|
||||
let updatedConversationHistoryDeletedRange = false
|
||||
let summaryResult:
|
||||
| {
|
||||
summaryText: string
|
||||
cost?: number
|
||||
tokensIn?: number
|
||||
tokensOut?: number
|
||||
cacheReads?: number
|
||||
cacheWrites?: number
|
||||
}
|
||||
| undefined
|
||||
|
||||
// If the previous API request's total token usage is close to the context window, truncate the conversation history to free up space for the new request
|
||||
// If the previous API request's total token usage is close to the context window, summarize the conversation history to free up space for the new request
|
||||
if (previousApiReqIndex >= 0) {
|
||||
const previousRequest = clineMessages[previousApiReqIndex]
|
||||
if (previousRequest && previousRequest.text) {
|
||||
@@ -129,47 +142,75 @@ export class ContextManager {
|
||||
|
||||
// This is the most reliable way to know when we're close to hitting the context window.
|
||||
if (totalTokens >= maxAllowedSize) {
|
||||
// Since the user may switch between models with different context windows, truncating half may not be enough (ie if switching from claude 200k to deepseek 64k, half truncation will only remove 100k tokens, but we need to remove much more)
|
||||
// So if totalTokens/2 is greater than maxAllowedSize, we truncate 3/4 instead of 1/2
|
||||
const keep = totalTokens / 2 > maxAllowedSize ? "quarter" : "half"
|
||||
|
||||
// we later check how many chars we trim to determine if we should still truncate history
|
||||
let [anyContextUpdates, uniqueFileReadIndices] = this.applyContextOptimizations(
|
||||
apiConversationHistory,
|
||||
conversationHistoryDeletedRange ? conversationHistoryDeletedRange[1] + 1 : 2,
|
||||
timestamp,
|
||||
)
|
||||
|
||||
let needToTruncate = true
|
||||
if (anyContextUpdates) {
|
||||
// determine whether we've saved enough chars to not truncate
|
||||
const charactersSavedPercentage = this.calculateContextOptimizationMetrics(
|
||||
try {
|
||||
// Use summarization instead of truncation
|
||||
summaryResult = await this.summarizationService.createSummary(
|
||||
apiConversationHistory,
|
||||
api,
|
||||
conversationHistoryDeletedRange,
|
||||
uniqueFileReadIndices,
|
||||
)
|
||||
if (charactersSavedPercentage >= 0.3) {
|
||||
needToTruncate = false
|
||||
}
|
||||
}
|
||||
|
||||
if (needToTruncate) {
|
||||
// go ahead with truncation
|
||||
anyContextUpdates = this.applyStandardContextTruncationNoticeChange(timestamp) || anyContextUpdates
|
||||
|
||||
// NOTE: it's okay that we overwriteConversationHistory in resume task since we're only ever removing the last user message and not anything in the middle which would affect this range
|
||||
conversationHistoryDeletedRange = this.getNextTruncationRange(
|
||||
apiConversationHistory,
|
||||
conversationHistoryDeletedRange,
|
||||
keep,
|
||||
)
|
||||
|
||||
// Create new conversation history with summary
|
||||
const summarizedHistory = this.summarizationService.createSummarizedConversationHistory(
|
||||
apiConversationHistory,
|
||||
summaryResult.summaryText,
|
||||
)
|
||||
|
||||
// Update the conversation history deleted range to indicate everything was summarized
|
||||
// Keep first user message (index 0) and replace everything else with summary (starting from index 1)
|
||||
conversationHistoryDeletedRange = [1, apiConversationHistory.length - 1]
|
||||
updatedConversationHistoryDeletedRange = true
|
||||
}
|
||||
|
||||
// if we alter the context history, save the updated version to disk
|
||||
if (anyContextUpdates) {
|
||||
await this.saveContextHistory(taskDirectory)
|
||||
return {
|
||||
conversationHistoryDeletedRange: conversationHistoryDeletedRange,
|
||||
updatedConversationHistoryDeletedRange: updatedConversationHistoryDeletedRange,
|
||||
truncatedConversationHistory: summarizedHistory,
|
||||
summaryResult: summaryResult,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to create conversation summary, falling back to truncation:", error)
|
||||
|
||||
// Fall back to original truncation logic
|
||||
const keep = totalTokens / 2 > maxAllowedSize ? "quarter" : "half"
|
||||
|
||||
// we later check how many chars we trim to determine if we should still truncate history
|
||||
let [anyContextUpdates, uniqueFileReadIndices] = this.applyContextOptimizations(
|
||||
apiConversationHistory,
|
||||
conversationHistoryDeletedRange ? conversationHistoryDeletedRange[1] + 1 : 2,
|
||||
timestamp,
|
||||
)
|
||||
|
||||
let needToTruncate = true
|
||||
if (anyContextUpdates) {
|
||||
// determine whether we've saved enough chars to not truncate
|
||||
const charactersSavedPercentage = this.calculateContextOptimizationMetrics(
|
||||
apiConversationHistory,
|
||||
conversationHistoryDeletedRange,
|
||||
uniqueFileReadIndices,
|
||||
)
|
||||
if (charactersSavedPercentage >= 0.3) {
|
||||
needToTruncate = false
|
||||
}
|
||||
}
|
||||
|
||||
if (needToTruncate) {
|
||||
// go ahead with truncation
|
||||
anyContextUpdates = this.applyStandardContextTruncationNoticeChange(timestamp) || anyContextUpdates
|
||||
|
||||
// NOTE: it's okay that we overwriteConversationHistory in resume task since we're only ever removing the last user message and not anything in the middle which would affect this range
|
||||
conversationHistoryDeletedRange = this.getNextTruncationRange(
|
||||
apiConversationHistory,
|
||||
conversationHistoryDeletedRange,
|
||||
keep,
|
||||
)
|
||||
|
||||
updatedConversationHistoryDeletedRange = true
|
||||
}
|
||||
|
||||
// if we alter the context history, save the updated version to disk
|
||||
if (anyContextUpdates) {
|
||||
await this.saveContextHistory(taskDirectory)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,6 +225,7 @@ export class ContextManager {
|
||||
conversationHistoryDeletedRange: conversationHistoryDeletedRange,
|
||||
updatedConversationHistoryDeletedRange: updatedConversationHistoryDeletedRange,
|
||||
truncatedConversationHistory: truncatedConversationHistory,
|
||||
summaryResult: summaryResult,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import { ApiHandler } from "@api/index"
|
||||
import { ClineApiReqInfo } from "@shared/ExtensionMessage"
|
||||
|
||||
export interface SummarizationResult {
|
||||
summaryText: string
|
||||
cost?: number
|
||||
tokensIn?: number
|
||||
tokensOut?: number
|
||||
cacheReads?: number
|
||||
cacheWrites?: number
|
||||
}
|
||||
|
||||
export class SummarizationService {
|
||||
private static readonly SUMMARIZATION_PROMPT = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
|
||||
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
|
||||
|
||||
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
|
||||
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
||||
- The user's explicit requests and intents
|
||||
- Your approach to addressing the user's requests
|
||||
- Key decisions, technical concepts and code patterns
|
||||
- Specific details like file names, full code snippets, function signatures, file edits, etc
|
||||
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
|
||||
|
||||
Your summary should include the following sections:
|
||||
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
||||
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
||||
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
|
||||
4. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
||||
5. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
||||
6. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
|
||||
7. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests without confirming with the user first.
|
||||
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
|
||||
|
||||
Here's an example of how your output should be structured:
|
||||
<example>
|
||||
<analysis>
|
||||
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
||||
</analysis>
|
||||
|
||||
<summary>
|
||||
1. Primary Request and Intent:
|
||||
[Detailed description]
|
||||
2. Key Technical Concepts:
|
||||
- [Concept 1]
|
||||
- [Concept 2]
|
||||
- [...]
|
||||
3. Files and Code Sections:
|
||||
- [File Name 1]
|
||||
- [Summary of why this file is important]
|
||||
- [Summary of the changes made to this file, if any]
|
||||
- [Important Code Snippet]
|
||||
- [File Name 2]
|
||||
- [Important Code Snippet]
|
||||
- [...]
|
||||
4. Problem Solving:
|
||||
[Description of solved problems and ongoing troubleshooting]
|
||||
5. Pending Tasks:
|
||||
- [Task 1]
|
||||
- [Task 2]
|
||||
- [...]
|
||||
6. Current Work:
|
||||
[Precise description of current work]
|
||||
7. Optional Next Step:
|
||||
[Optional Next step to take]
|
||||
</summary>
|
||||
</example>
|
||||
|
||||
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.`
|
||||
|
||||
private static readonly CONTINUATION_PROMPT = `This session is being continued from a previous conversation that ran out of context. The conversation is summarized above.
|
||||
|
||||
Please continue the conversation from where we left it off without asking the user any further questions. Continue with the last task that you were asked to work on.`
|
||||
|
||||
async createSummary(
|
||||
apiConversationHistory: Anthropic.Messages.MessageParam[],
|
||||
api: ApiHandler,
|
||||
conversationHistoryDeletedRange: [number, number] | undefined,
|
||||
): Promise<SummarizationResult> {
|
||||
try {
|
||||
// Create the summarization request using the same conversation history
|
||||
// This leverages the existing prompt cache for cost efficiency
|
||||
const summaryMessages: Anthropic.Messages.MessageParam[] = [
|
||||
...apiConversationHistory,
|
||||
{
|
||||
role: "user",
|
||||
content: SummarizationService.SUMMARIZATION_PROMPT,
|
||||
},
|
||||
]
|
||||
|
||||
// Use the existing API handler to make the request
|
||||
const stream = api.createMessage("", summaryMessages)
|
||||
|
||||
let summaryText = ""
|
||||
let cost: number | undefined
|
||||
let tokensIn: number | undefined
|
||||
let tokensOut: number | undefined
|
||||
let cacheReads: number | undefined
|
||||
let cacheWrites: number | undefined
|
||||
|
||||
// Collect the streamed response
|
||||
for await (const chunk of stream) {
|
||||
if (chunk.type === "text") {
|
||||
summaryText += chunk.text
|
||||
} else if (chunk.type === "usage") {
|
||||
// Extract token usage and cost information
|
||||
tokensIn = chunk.inputTokens
|
||||
tokensOut = chunk.outputTokens
|
||||
cacheReads = chunk.cacheReadTokens
|
||||
cacheWrites = chunk.cacheWriteTokens
|
||||
|
||||
// Calculate cost using the same logic as API requests
|
||||
if (tokensIn !== undefined && tokensOut !== undefined) {
|
||||
const modelInfo = api.getModel().info
|
||||
if (modelInfo.inputPrice !== undefined && modelInfo.outputPrice !== undefined) {
|
||||
cost = modelInfo.inputPrice * (tokensIn / 1_000_000) + modelInfo.outputPrice * (tokensOut / 1_000_000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
summaryText,
|
||||
cost,
|
||||
tokensIn,
|
||||
tokensOut,
|
||||
cacheReads,
|
||||
cacheWrites,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error creating conversation summary:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
createSummarizedConversationHistory(
|
||||
originalHistory: Anthropic.Messages.MessageParam[],
|
||||
summaryText: string,
|
||||
): Anthropic.Messages.MessageParam[] {
|
||||
// Keep the system prompt (if exists) and first user message
|
||||
const systemMessage = originalHistory.length > 0 && originalHistory[0].role === "user" ? originalHistory[0] : null
|
||||
|
||||
// Create the new conversation history with summary
|
||||
const summarizedHistory: Anthropic.Messages.MessageParam[] = []
|
||||
|
||||
if (systemMessage) {
|
||||
summarizedHistory.push(systemMessage)
|
||||
}
|
||||
|
||||
// Add the summary as a user message
|
||||
summarizedHistory.push({
|
||||
role: "user",
|
||||
content: summaryText,
|
||||
})
|
||||
|
||||
// Add the continuation instruction as a user message
|
||||
summarizedHistory.push({
|
||||
role: "user",
|
||||
content: SummarizationService.CONTINUATION_PROMPT,
|
||||
})
|
||||
|
||||
return summarizedHistory
|
||||
}
|
||||
}
|
||||
@@ -1746,6 +1746,25 @@ export class Task {
|
||||
await ensureTaskDirectoryExists(this.getContext(), this.taskId),
|
||||
)
|
||||
|
||||
// Handle summarization result if it occurred
|
||||
if (contextManagementMetadata.summaryResult) {
|
||||
// Add a conversation summary message to the ClineMessages
|
||||
const summaryMessage: ClineMessage = {
|
||||
ts: Date.now(),
|
||||
type: "say",
|
||||
say: "conversation_summary",
|
||||
text: JSON.stringify({
|
||||
cost: contextManagementMetadata.summaryResult.cost,
|
||||
tokensIn: contextManagementMetadata.summaryResult.tokensIn,
|
||||
tokensOut: contextManagementMetadata.summaryResult.tokensOut,
|
||||
cacheReads: contextManagementMetadata.summaryResult.cacheReads,
|
||||
cacheWrites: contextManagementMetadata.summaryResult.cacheWrites,
|
||||
} satisfies ClineApiReqInfo),
|
||||
}
|
||||
|
||||
await this.messageStateHandler.addToClineMessages(summaryMessage)
|
||||
}
|
||||
|
||||
if (contextManagementMetadata.updatedConversationHistoryDeletedRange) {
|
||||
this.taskState.conversationHistoryDeletedRange = contextManagementMetadata.conversationHistoryDeletedRange
|
||||
await this.messageStateHandler.saveClineMessagesAndUpdateHistory()
|
||||
|
||||
@@ -124,6 +124,7 @@ export type ClineSay =
|
||||
| "clineignore_error"
|
||||
| "checkpoint_created"
|
||||
| "load_mcp_documentation"
|
||||
| "conversation_summary"
|
||||
| "info" // Added for general informational messages like retry status
|
||||
|
||||
export interface ClineSayTool {
|
||||
|
||||
@@ -14,4 +14,5 @@ export type HistoryItem = {
|
||||
conversationHistoryDeletedRange?: [number, number]
|
||||
isFavorited?: boolean
|
||||
checkpointTrackerErrorMessage?: string
|
||||
conversationWasSummarized?: boolean
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ function convertClineSayToProtoEnum(say: AppClineSay | undefined): ClineSay | un
|
||||
clineignore_error: ClineSay.CLINEIGNORE_ERROR,
|
||||
checkpoint_created: ClineSay.CHECKPOINT_CREATED,
|
||||
load_mcp_documentation: ClineSay.LOAD_MCP_DOCUMENTATION,
|
||||
conversation_summary: ClineSay.CONVERSATION_SUMMARY,
|
||||
info: ClineSay.INFO,
|
||||
}
|
||||
|
||||
@@ -140,6 +141,7 @@ function convertProtoEnumToClineSay(say: ClineSay): AppClineSay | undefined {
|
||||
[ClineSay.CLINEIGNORE_ERROR]: "clineignore_error",
|
||||
[ClineSay.CHECKPOINT_CREATED]: "checkpoint_created",
|
||||
[ClineSay.LOAD_MCP_DOCUMENTATION]: "load_mcp_documentation",
|
||||
[ClineSay.CONVERSATION_SUMMARY]: "conversation_summary",
|
||||
[ClineSay.INFO]: "info",
|
||||
}
|
||||
|
||||
|
||||
@@ -1033,6 +1033,58 @@ export const ChatRowContent = memo(
|
||||
Loading MCP documentation
|
||||
</div>
|
||||
)
|
||||
case "conversation_summary":
|
||||
const summaryInfo: ClineApiReqInfo = message.text ? JSON.parse(message.text) : {}
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
style={{
|
||||
...headerStyle,
|
||||
marginBottom: "10px",
|
||||
justifyContent: "space-between",
|
||||
}}>
|
||||
<div style={{ display: "flex", alignItems: "center", gap: "10px" }}>
|
||||
<span
|
||||
className="codicon codicon-archive"
|
||||
style={{
|
||||
color: "var(--vscode-charts-blue)",
|
||||
marginBottom: "-1.5px",
|
||||
}}></span>
|
||||
<span style={{ color: "var(--vscode-charts-blue)", fontWeight: "bold" }}>
|
||||
Conversation Summarized
|
||||
</span>
|
||||
</div>
|
||||
{summaryInfo.cost !== undefined && (
|
||||
<div
|
||||
style={{
|
||||
fontSize: "12px",
|
||||
color: "var(--vscode-descriptionForeground)",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: "8px",
|
||||
}}>
|
||||
{summaryInfo.tokensIn && summaryInfo.tokensOut && (
|
||||
<span>
|
||||
{summaryInfo.tokensIn.toLocaleString()} →{" "}
|
||||
{summaryInfo.tokensOut.toLocaleString()} tokens
|
||||
</span>
|
||||
)}
|
||||
{summaryInfo.cacheReads && (
|
||||
<span>
|
||||
<i className="codicon codicon-database" style={{ marginRight: "2px" }} />
|
||||
{summaryInfo.cacheReads.toLocaleString()} cached
|
||||
</span>
|
||||
)}
|
||||
<span>${summaryInfo.cost.toFixed(4)}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<p style={pStyle}>
|
||||
The conversation history was summarized to free up context space. Previous messages have been
|
||||
consolidated into a summary to continue the conversation efficiently.
|
||||
</p>
|
||||
</>
|
||||
)
|
||||
case "completion_result":
|
||||
const hasChanges = message.text?.endsWith(COMPLETION_RESULT_CHANGES_FLAG) ?? false
|
||||
const text = hasChanges ? message.text?.slice(0, -COMPLETION_RESULT_CHANGES_FLAG.length) : message.text
|
||||
|
||||
Reference in New Issue
Block a user