Skip to content

Commit a7369b4

Browse files
committed
Preserve images from last user message during context pruning
1 parent 4e5d082 commit a7369b4

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

agents/context-pruner.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { publisher } from './constants'
22

33
import type { AgentDefinition, ToolCall } from './types/agent-definition'
4-
import type { Message, ToolMessage } from './types/util-types'
4+
import type {
5+
FilePart,
6+
ImagePart,
7+
Message,
8+
TextPart,
9+
ToolMessage,
10+
UserMessage,
11+
} from './types/util-types'
512

613
// =============================================================================
714
// Helper Functions (exported for testing)
@@ -682,6 +689,23 @@ const definition: AgentDefinition = {
682689
// Build the summary
683690
const summaryParts: string[] = []
684691

692+
// Find the last user message with images to preserve in the final output
693+
// We preserve the most recent user's images since they're likely the most relevant
694+
let lastUserImageParts: Array<Record<string, unknown>> = []
695+
for (let i = messagesWithoutOldSummaries.length - 1; i >= 0; i--) {
696+
const msg = messagesWithoutOldSummaries[i]
697+
if (msg.role === 'user' && Array.isArray(msg.content)) {
698+
const imageParts = msg.content.filter(
699+
(part: Record<string, unknown>) =>
700+
part.type === 'image' || part.type === 'media',
701+
)
702+
if (imageParts.length > 0) {
703+
lastUserImageParts = imageParts
704+
break
705+
}
706+
}
707+
}
708+
685709
// If there was a previous summary, include it first (no marker needed, already chronological)
686710
if (previousSummary) {
687711
summaryParts.push(previousSummary)
@@ -920,21 +944,27 @@ const definition: AgentDefinition = {
920944
}
921945

922946
// Create the summarized message with fresh sentAt timestamp
947+
// Include any images from the last user message that had images
923948
const now = Date.now()
924-
const summarizedMessage: Message = {
925-
role: 'user',
926-
content: [
927-
{
928-
type: 'text',
929-
text: `<conversation_summary>
949+
const textPart: TextPart = {
950+
type: 'text',
951+
text: `<conversation_summary>
930952
This is a summary of the conversation so far. The original messages have been condensed to save context space.
931953
932954
${summaryText}
933955
</conversation_summary>
934956
935957
Please continue the conversation from here. In particular, try to address the user's latest request detailed in the summary above. You may need to re-gather context (e.g. read some files) to get up to speed and then tackle the user's request.`,
936-
},
937-
],
958+
}
959+
// Build content array with text and any preserved images
960+
const summaryContentParts: (TextPart | ImagePart | FilePart)[] = [textPart]
961+
// Append image parts (they're already typed correctly from the original message)
962+
for (const part of lastUserImageParts) {
963+
summaryContentParts.push(part as ImagePart | FilePart)
964+
}
965+
const summarizedMessage: UserMessage = {
966+
role: 'user',
967+
content: summaryContentParts,
938968
sentAt: now,
939969
}
940970

0 commit comments

Comments
 (0)