Skip to content

Commit dad96e5

Browse files
ampagent5omeOtherGuy
authored andcommitted
fix(acp): keep v1 assistant images and standard tool images
Preserve bounded raster content through the ACP parser and reuse chat attachment storage for v1 assistant images. Keep segmented image replies visible in web and mobile without changing text-only folds or v2 authoritative assistant updates. Replaces #30101, rebuilt on current main. Text around an inline image now gets its own segment instead of reopening the closed same-messageId segment. Ported by Claude Opus 5.5 in Amp. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d49f-af4b-77bf-b0f4-ad0d620d8ca0 Co-authored-by: Amp <[email protected]>
1 parent 7903b48 commit dad96e5

12 files changed

Lines changed: 661 additions & 98 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChatAttachmentId,
23
MessageId,
34
CheckpointId,
45
CheckpointRef,
@@ -941,25 +942,45 @@ describe("buildThreadFeed", () => {
941942
).toBe(null);
942943
});
943944

944-
it("folds progress messages while keeping the final assistant answer visible", () => {
945+
it.each([false, true])("folds progress before a reply with images: %s", (hasImages) => {
946+
const attachments = hasImages
947+
? [
948+
{
949+
type: "image" as const,
950+
id: ChatAttachmentId.make("reply-image"),
951+
name: "Reply",
952+
mimeType: "image/png",
953+
sizeBytes: 401,
954+
},
955+
]
956+
: [];
945957
const opening = {
946958
...assistantMessage("2026-06-20T00:00:01.500Z"),
947959
id: TurnItemId.make("item-opening"),
948960
messageId: MessageId.make("message-opening"),
949961
text: "I will check the deployment configuration.",
962+
attachments,
950963
};
951964
const middle = {
952965
...assistantMessage("2026-06-20T00:00:02.500Z"),
953966
id: TurnItemId.make("item-middle"),
954967
messageId: MessageId.make("message-middle"),
955968
text: "The configuration is valid; checking the build next.",
956969
};
970+
const image = {
971+
...assistantMessage("2026-06-20T00:00:02.750Z"),
972+
id: TurnItemId.make("item-image"),
973+
messageId: MessageId.make("message-image"),
974+
text: hasImages ? "" : "Build is finishing.",
975+
attachments,
976+
};
957977
const feed = buildThreadFeed([
958978
projected(userMessage(), 0),
959979
projected(opening, 1),
960980
projected(command(), 2),
961981
projected(middle, 3),
962-
projected(assistantMessage(), 4),
982+
projected(image, 4),
983+
projected(assistantMessage(), 5),
963984
]);
964985
const latestRun = {
965986
runId,
@@ -972,6 +993,7 @@ describe("buildThreadFeed", () => {
972993
expect(collapsed.map((entry) => entry.id)).toEqual([
973994
"message-user",
974995
"run-fold:run-1",
996+
...(hasImages ? ["message-middle", "message-image"] : []),
975997
"message-assistant",
976998
]);
977999
expect(collapsed[1]).toMatchObject({
@@ -988,6 +1010,7 @@ describe("buildThreadFeed", () => {
9881010
"activity-group",
9891011
"message",
9901012
"message",
1013+
"message",
9911014
]);
9921015
const anchor = {
9931016
...command(),
@@ -1015,8 +1038,35 @@ describe("buildThreadFeed", () => {
10151038
"message-user",
10161039
"message-opening",
10171040
"message-middle",
1041+
"message-image",
10181042
"message-assistant",
10191043
]);
1044+
1045+
const steered = deriveThreadFeedPresentation(
1046+
buildThreadFeed([
1047+
projected(userMessage(), 0),
1048+
projected(opening, 1),
1049+
projected(
1050+
{
1051+
...userMessage("2026-06-20T00:00:02.000Z"),
1052+
id: TurnItemId.make("item-steer"),
1053+
messageId: MessageId.make("message-steer"),
1054+
inputIntent: "steer",
1055+
},
1056+
2,
1057+
),
1058+
projected(middle, 3),
1059+
projected(image, 4),
1060+
projected(assistantMessage(), 5),
1061+
]),
1062+
latestRun,
1063+
new Set(),
1064+
);
1065+
expect(
1066+
steered.flatMap((entry) =>
1067+
entry.type === "message" && entry.message.role === "assistant" ? [entry.id] : [],
1068+
),
1069+
).toEqual([...(hasImages ? ["message-middle", "message-image"] : []), "message-assistant"]);
10201070
});
10211071

10221072
// regression: #21393 show plans and fold settled progress notes

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,11 +1220,29 @@ function deriveThreadFeedRunFolds(
12201220
const visibleCompactionId = group.entries.findLast(
12211221
(entry) => entry.type === "activity-group" && isContextCompactionActivityGroup(entry),
12221222
)?.id;
1223+
const terminalEntryIndex = terminalAssistantId
1224+
? group.entries.findIndex((entry) => entry.id === terminalAssistantId)
1225+
: group.entries.length;
1226+
const responseBoundaryIndex =
1227+
group.steerSegments.findLast(({ startIndex }) => startIndex <= terminalEntryIndex)
1228+
?.startIndex ?? 0;
1229+
let responseStartIndex = terminalEntryIndex;
1230+
let responseHasImages = false;
1231+
// Inline images split one reply into consecutive text and attachment messages.
1232+
for (let index = terminalEntryIndex; index >= responseBoundaryIndex; index--) {
1233+
const entry = group.entries[index];
1234+
if (entry?.type !== "message" || entry.message.role !== "assistant") break;
1235+
responseStartIndex = index;
1236+
if (entry.message.attachments.some((attachment) => attachment.type === "image")) {
1237+
responseHasImages = true;
1238+
}
1239+
}
1240+
if (!responseHasImages) responseStartIndex = terminalEntryIndex;
12231241
const hiddenEntryIds = new Set(
12241242
group.entries
12251243
.filter(
1226-
(entry) =>
1227-
entry.id !== terminalAssistantId &&
1244+
(entry, index) =>
1245+
(index < responseStartIndex || index > terminalEntryIndex) &&
12281246
!(entry.type === "message" && retainedAnswerIds.has(entry.message.id)) &&
12291247
entry.type !== "html-render" &&
12301248
entry.type !== "mcp-app" &&
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
ChatAttachmentId,
3+
PersistChatAttachmentsError,
4+
type PersistChatAttachmentsInput,
5+
} from "@t3tools/contracts";
6+
import * as Base64 from "effect/encoding/Base64";
7+
import * as Effect from "effect/Effect";
8+
import * as FileSystem from "effect/FileSystem";
9+
import * as Path from "effect/Path";
10+
11+
import {
12+
attachmentRelativePath,
13+
chatAttachmentStableKey,
14+
createDeterministicAttachmentId,
15+
} from "../attachmentStore.ts";
16+
import * as ServerConfig from "../config.ts";
17+
import { parseBase64DataUrl } from "../imageMime.ts";
18+
19+
export const persistChatAttachments = Effect.fn("ws.assets.persistChatAttachments")(function* (
20+
input: PersistChatAttachmentsInput,
21+
) {
22+
const config = yield* ServerConfig.ServerConfig;
23+
const fileSystem = yield* FileSystem.FileSystem;
24+
const path = yield* Path.Path;
25+
return yield* Effect.forEach(
26+
input.attachments.map((attachment, index) => ({ attachment, index })),
27+
Effect.fn("ws.assets.persistChatAttachment")(function* ({ attachment, index }) {
28+
const parsed = parseBase64DataUrl(attachment.dataUrl);
29+
if (parsed === null || parsed.mimeType !== attachment.mimeType.toLowerCase()) {
30+
return yield* new PersistChatAttachmentsError({
31+
message: `Attachment ${attachment.name} has an invalid image payload.`,
32+
});
33+
}
34+
const bytes = yield* Effect.fromResult(Base64.decode(parsed.base64)).pipe(
35+
Effect.mapError(
36+
(cause) =>
37+
new PersistChatAttachmentsError({
38+
message: `Attachment ${attachment.name} is not valid base64.`,
39+
cause,
40+
}),
41+
),
42+
);
43+
if (bytes.byteLength !== attachment.sizeBytes) {
44+
return yield* new PersistChatAttachmentsError({
45+
message: `Attachment ${attachment.name} size does not match its payload.`,
46+
});
47+
}
48+
const rawId = createDeterministicAttachmentId(
49+
input.threadId,
50+
chatAttachmentStableKey(input.messageId, index, bytes),
51+
);
52+
if (rawId === null) {
53+
return yield* new PersistChatAttachmentsError({
54+
message: "Could not allocate an attachment identifier.",
55+
});
56+
}
57+
const persisted = {
58+
type: "image" as const,
59+
id: ChatAttachmentId.make(rawId),
60+
name: attachment.name,
61+
mimeType: attachment.mimeType,
62+
sizeBytes: attachment.sizeBytes,
63+
};
64+
yield* fileSystem
65+
.writeFile(path.join(config.attachmentsDir, attachmentRelativePath(persisted)!), bytes)
66+
.pipe(
67+
Effect.mapError(
68+
(cause) =>
69+
new PersistChatAttachmentsError({
70+
message: `Could not persist attachment ${attachment.name}.`,
71+
cause,
72+
}),
73+
),
74+
);
75+
return persisted;
76+
}),
77+
{ concurrency: 2 },
78+
);
79+
});

0 commit comments

Comments
 (0)