|
1 | 1 | import { publisher } from './constants' |
2 | 2 |
|
3 | 3 | 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' |
5 | 12 |
|
6 | 13 | // ============================================================================= |
7 | 14 | // Helper Functions (exported for testing) |
@@ -682,6 +689,23 @@ const definition: AgentDefinition = { |
682 | 689 | // Build the summary |
683 | 690 | const summaryParts: string[] = [] |
684 | 691 |
|
| 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 | + |
685 | 709 | // If there was a previous summary, include it first (no marker needed, already chronological) |
686 | 710 | if (previousSummary) { |
687 | 711 | summaryParts.push(previousSummary) |
@@ -920,21 +944,27 @@ const definition: AgentDefinition = { |
920 | 944 | } |
921 | 945 |
|
922 | 946 | // Create the summarized message with fresh sentAt timestamp |
| 947 | + // Include any images from the last user message that had images |
923 | 948 | 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> |
930 | 952 | This is a summary of the conversation so far. The original messages have been condensed to save context space. |
931 | 953 |
|
932 | 954 | ${summaryText} |
933 | 955 | </conversation_summary> |
934 | 956 |
|
935 | 957 | 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, |
938 | 968 | sentAt: now, |
939 | 969 | } |
940 | 970 |
|
|
0 commit comments