truncate first user message (#5668)
This commit is contained in:
1 parent
d6935623f9
commit
54faddb3ff
4 files changed
+53
-17
No files matched your search
@@ -399,14 +399,14 @@ export class ContextManager {
|
||||
* Public function for triggering potentially setting the truncation message
|
||||
* If the truncation message already exists, does nothing, otherwise adds the message
|
||||
*/
|
||||
async triggerApplyStandardContextTruncationNoticeChange(timestamp: number, taskDirectory: string) {
|
||||
/*
|
||||
const assistantUpdated = this.applyStandardContextTruncationNoticeChange(timestamp)
|
||||
const userUpdated = this.applyFirstUserMessageReplacement(timestamp)
|
||||
if (assistantUpdated || userUpdated)
|
||||
*/
|
||||
const updated = this.applyStandardContextTruncationNoticeChange(timestamp)
|
||||
if (updated) {
|
||||
async triggerApplyStandardContextTruncationNoticeChange(
|
||||
timestamp: number,
|
||||
taskDirectory: string,
|
||||
apiConversationHistory: Anthropic.Messages.MessageParam[],
|
||||
) {
|
||||
const assistantUpdated = this.applyStandardContextTruncationNoticeChange(timestamp)
|
||||
const userUpdated = this.applyFirstUserMessageReplacement(timestamp, apiConversationHistory)
|
||||
if (assistantUpdated || userUpdated) {
|
||||
await this.saveContextHistory(taskDirectory)
|
||||
}
|
||||
}
|
||||
@@ -428,13 +428,35 @@ export class ContextManager {
|
||||
/**
|
||||
* Replace the first user message when context window is compacted
|
||||
*/
|
||||
private applyFirstUserMessageReplacement(timestamp: number): boolean {
|
||||
private applyFirstUserMessageReplacement(
|
||||
timestamp: number,
|
||||
apiConversationHistory: Anthropic.Messages.MessageParam[],
|
||||
): boolean {
|
||||
if (!this.contextHistoryUpdates.has(0)) {
|
||||
// first user message always at index 0
|
||||
const innerMap = new Map<number, ContextUpdate[]>()
|
||||
innerMap.set(0, [[timestamp, "text", [formatResponse.contextTruncationFirstUserMessage()], []]])
|
||||
this.contextHistoryUpdates.set(0, [0, innerMap]) // same EditType as first assistant truncation notice
|
||||
return true
|
||||
try {
|
||||
// choosing to be extra careful here, but likely not required
|
||||
let firstUserMessage = ""
|
||||
|
||||
const message = apiConversationHistory[0]
|
||||
if (Array.isArray(message.content)) {
|
||||
const block = message.content[0]
|
||||
if (block && block.type === "text") {
|
||||
firstUserMessage = block.text
|
||||
}
|
||||
}
|
||||
|
||||
if (firstUserMessage) {
|
||||
const processedFirstUserMessage = formatResponse.processFirstUserMessageForTruncation(firstUserMessage)
|
||||
|
||||
const innerMap = new Map<number, ContextUpdate[]>()
|
||||
innerMap.set(0, [[timestamp, "text", [processedFirstUserMessage], []]])
|
||||
this.contextHistoryUpdates.set(0, [0, innerMap]) // same EditType as first assistant truncation notice
|
||||
|
||||
return true
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("applyFirstUserMessageReplacement:", error)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -9,9 +9,18 @@ export const formatResponse = {
|
||||
`[[NOTE] This file read has been removed to save space in the context window. Refer to the latest file read for the most up to date version of this file.]`,
|
||||
|
||||
contextTruncationNotice: () =>
|
||||
`[NOTE] Some previous conversation history with the user has been removed to maintain optimal context window length. The initial user task and the most recent exchanges have been retained for continuity, while intermediate conversation history has been removed. Please keep this in mind as you continue assisting the user.`,
|
||||
`[NOTE] Some previous conversation history with the user has been removed to maintain optimal context window length. The initial user task has been retained for continuity, while intermediate conversation history has been removed. Please keep this in mind as you continue assisting the user.`,
|
||||
|
||||
contextTruncationFirstUserMessage: () => `It's time to build!`,
|
||||
processFirstUserMessageForTruncation: (originalContent: string) => {
|
||||
const MAX_CHARS = 400_000
|
||||
|
||||
if (originalContent.length <= MAX_CHARS) {
|
||||
return originalContent
|
||||
}
|
||||
|
||||
const truncated = originalContent.substring(0, MAX_CHARS)
|
||||
return truncated + "\n\n[[NOTE] This message was truncated past this point to preserve context window space.]"
|
||||
},
|
||||
|
||||
condense: () =>
|
||||
`The user has accepted the condensed conversation summary you generated. This summary covers important details of the historical conversation with the user which has been truncated.\n<explicit_instructions type="condense_response">It's crucial that you respond by ONLY asking the user what you should work on next. You should NOT take any initiative or make any assumptions about continuing with work. For example you should NOT suggest file changes or attempt to read any files.\nWhen asking the user what you should work on next, you can reference information in the summary which was just generated. However, you should NOT reference information outside of what's contained in the summary for this response. Keep this response CONCISE.</explicit_instructions>`,
|
||||
|
||||
@@ -1724,6 +1724,7 @@ export class ToolExecutor {
|
||||
await this.contextManager.triggerApplyStandardContextTruncationNoticeChange(
|
||||
Date.now(),
|
||||
await ensureTaskDirectoryExists(this.context, this.taskId),
|
||||
apiConversationHistory,
|
||||
)
|
||||
}
|
||||
await this.saveCheckpoint()
|
||||
@@ -1812,6 +1813,7 @@ export class ToolExecutor {
|
||||
await this.contextManager.triggerApplyStandardContextTruncationNoticeChange(
|
||||
Date.now(),
|
||||
await ensureTaskDirectoryExists(this.context, this.taskId),
|
||||
apiConversationHistory,
|
||||
)
|
||||
}
|
||||
await this.saveCheckpoint()
|
||||
|
||||
@@ -1622,8 +1622,10 @@ export class Task {
|
||||
}
|
||||
|
||||
private async handleContextWindowExceededError(): Promise<void> {
|
||||
const apiConversationHistory = this.messageStateHandler.getApiConversationHistory()
|
||||
|
||||
this.taskState.conversationHistoryDeletedRange = this.contextManager.getNextTruncationRange(
|
||||
this.messageStateHandler.getApiConversationHistory(),
|
||||
apiConversationHistory,
|
||||
this.taskState.conversationHistoryDeletedRange,
|
||||
"quarter", // Force aggressive truncation
|
||||
)
|
||||
@@ -1631,6 +1633,7 @@ export class Task {
|
||||
await this.contextManager.triggerApplyStandardContextTruncationNoticeChange(
|
||||
Date.now(),
|
||||
await ensureTaskDirectoryExists(this.getContext(), this.taskId),
|
||||
apiConversationHistory,
|
||||
)
|
||||
|
||||
this.taskState.didAutomaticallyRetryFailedApiRequest = true
|
||||
|
||||
Reference in new issue
Block a user