Skip to content

Commit 159556f

Browse files
author
martin
committed
feat(reply): annotate recent history images
1 parent 41bea62 commit 159556f

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎src/auto-reply/reply/dispatch-acp.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,24 +822,36 @@ describe("tryDispatchAcpReply", () => {
822822
path: "/tmp/recent-2.png",
823823
contentType: "image/png",
824824
sender: "Recent 2",
825+
sentAtMs: 1_699_999_997_000,
826+
messagePosition: 6,
827+
messageCount: 9,
825828
messageId: "recent-2",
826829
},
827830
{
828831
path: "/tmp/recent-3.png",
829832
contentType: "image/png",
830833
sender: "Recent 3",
834+
sentAtMs: 1_699_999_998_000,
835+
messagePosition: 7,
836+
messageCount: 9,
831837
messageId: "recent-3",
832838
},
833839
{
834840
path: "/tmp/recent-4.png",
835841
contentType: "image/png",
836842
sender: "Recent 4",
843+
sentAtMs: 1_699_999_999_000,
844+
messagePosition: 8,
845+
messageCount: 9,
837846
messageId: "recent-4",
838847
},
839848
{
840849
path: "C:\\Users\\Alice\\Pictures\\recent.png",
841850
contentType: "image/png",
842851
sender: "Windows",
852+
sentAtMs: 1_699_999_999_500,
853+
messagePosition: 9,
854+
messageCount: 9,
843855
messageId: "windows",
844856
},
845857
]);
@@ -853,13 +865,18 @@ describe("tryDispatchAcpReply", () => {
853865
path: "/tmp/secret.png",
854866
contentType: "image/png",
855867
sender: "@alice",
868+
sentAtMs: 1_700_000_000_000,
869+
messagePosition: 2,
870+
messageCount: 5,
856871
messageId: "msg-1",
857872
},
858873
],
859874
});
860875

861876
expect(text).toContain("what is this?");
862877
expect(text).toContain("Recent image 1 from @alice, message msg-1");
878+
expect(text).toContain("sent at 2023-11-14T22:13:20.000Z");
879+
expect(text).toContain("thread message 2 of 5");
863880
expect(text).not.toContain("/tmp/secret.png");
864881
});
865882

@@ -915,6 +932,9 @@ describe("tryDispatchAcpReply", () => {
915932
path: imagePath,
916933
contentType: "image/png",
917934
sender: "@alice",
935+
sentAtMs: 1_700_000_000_000,
936+
messagePosition: 1,
937+
messageCount: 1,
918938
messageId: "msg-1",
919939
},
920940
]);
@@ -1159,6 +1179,9 @@ describe("tryDispatchAcpReply", () => {
11591179
path: historyPath,
11601180
contentType: "image/png",
11611181
sender: "@alice",
1182+
sentAtMs: 1_700_000_000_000,
1183+
messagePosition: 1,
1184+
messageCount: 1,
11621185
messageId: "msg-history",
11631186
},
11641187
]);
@@ -1195,6 +1218,54 @@ describe("tryDispatchAcpReply", () => {
11951218
]);
11961219
});
11971220

1221+
it("annotates recent history images with sent time and thread position in ACP turns", async () => {
1222+
setReadyAcpResolution();
1223+
const historyPath = "/tmp/openclaw-history-metadata.png";
1224+
const historyImage = Buffer.from("history-image");
1225+
acpAttachmentBuffers.set(historyPath, historyImage);
1226+
1227+
await runDispatch({
1228+
bodyForAgent: "describe current state",
1229+
ctxOverrides: {
1230+
Timestamp: 1_700_000_060_000,
1231+
InboundHistory: [
1232+
{
1233+
sender: "@alice",
1234+
body: "bug report",
1235+
timestamp: 1_699_999_980_000,
1236+
messageId: "msg-before",
1237+
},
1238+
{
1239+
sender: "@bob",
1240+
body: "<media:image>",
1241+
timestamp: 1_700_000_000_000,
1242+
messageId: "msg-history",
1243+
media: [{ path: historyPath, contentType: "image/png", kind: "image" }],
1244+
},
1245+
{
1246+
sender: "@alice",
1247+
body: "fixed after refresh",
1248+
timestamp: 1_700_000_060_000,
1249+
messageId: "msg-after",
1250+
},
1251+
],
1252+
},
1253+
});
1254+
1255+
const text = String(runTurnCall().text);
1256+
expect(text).toContain("describe current state");
1257+
expect(text).toContain("Recent image 1 from @bob, message msg-history");
1258+
expect(text).toContain("sent at 2023-11-14T22:13:20.000Z");
1259+
expect(text).toContain("thread message 2 of 3");
1260+
expect(text).not.toContain(historyPath);
1261+
expect(runTurnCall().attachments).toEqual([
1262+
{
1263+
mediaType: "image/png",
1264+
data: historyImage.toString("base64"),
1265+
},
1266+
]);
1267+
});
1268+
11981269
it("forwards media-understanding PDF page images alongside current image attachments", async () => {
11991270
setReadyAcpResolution();
12001271
const currentPath = "/tmp/openclaw-current-image.png";

‎src/auto-reply/reply/history-media.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export type RecentInboundHistoryImage = {
1212
path: string;
1313
contentType: string;
1414
sender: string;
15+
sentAtMs: number;
16+
messagePosition: number;
17+
messageCount: number;
1518
messageId?: string;
1619
};
1720

@@ -100,13 +103,21 @@ export function resolveRecentInboundHistoryImages(params: {
100103
path: mediaPath,
101104
contentType,
102105
sender: entry.sender,
106+
sentAtMs: timestamp,
107+
messagePosition: index + 1,
108+
messageCount: entries.length,
103109
...(messageId ? { messageId } : {}),
104110
});
105111
}
106112
}
107113
return out.toReversed();
108114
}
109115

116+
function formatRecentHistoryImageSentAt(sentAtMs: number): string {
117+
const date = new Date(sentAtMs);
118+
return Number.isFinite(date.getTime()) ? date.toISOString() : `${sentAtMs}ms since epoch`;
119+
}
120+
110121
export function appendRecentHistoryImageContext(params: {
111122
promptText: string;
112123
images: RecentInboundHistoryImage[];
@@ -116,7 +127,8 @@ export function appendRecentHistoryImageContext(params: {
116127
}
117128
const notes = params.images.map((image, index) => {
118129
const message = image.messageId ? `, message ${image.messageId}` : "";
119-
return `[Recent image ${index + 1} from ${image.sender}${message}, attached as media.]`;
130+
const sentAt = formatRecentHistoryImageSentAt(image.sentAtMs);
131+
return `[Recent image ${index + 1} from ${image.sender}${message}, sent at ${sentAt}, thread message ${image.messagePosition} of ${image.messageCount}, attached as media.]`;
120132
});
121133
return [params.promptText, notes.join("\n")]
122134
.filter((part) => part.trim().length > 0)

0 commit comments

Comments
 (0)