Skip to content

Commit ba82410

Browse files
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. Written with Amp High in an Orb; the writing model name was not recorded. Amp-Thread-ID: T-01a1a0b3-0b97-77d4-9fbf-85ca74a651ca Co-authored-by: 5omeOtherGuy <[email protected]>
1 parent cefb544 commit ba82410

12 files changed

Lines changed: 652 additions & 99 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,
@@ -810,25 +811,45 @@ describe("buildThreadFeed", () => {
810811
);
811812
});
812813

813-
it("folds progress messages while keeping the final assistant answer visible", () => {
814+
it.each([false, true])("folds progress before a reply with images: %s", (hasImages) => {
815+
const attachments = hasImages
816+
? [
817+
{
818+
type: "image" as const,
819+
id: ChatAttachmentId.make("reply-image"),
820+
name: "Reply",
821+
mimeType: "image/png",
822+
sizeBytes: 401,
823+
},
824+
]
825+
: [];
814826
const opening = {
815827
...assistantMessage("2026-06-20T00:00:01.500Z"),
816828
id: TurnItemId.make("item-opening"),
817829
messageId: MessageId.make("message-opening"),
818830
text: "I will check the deployment configuration.",
831+
attachments,
819832
};
820833
const middle = {
821834
...assistantMessage("2026-06-20T00:00:02.500Z"),
822835
id: TurnItemId.make("item-middle"),
823836
messageId: MessageId.make("message-middle"),
824837
text: "The configuration is valid; checking the build next.",
825838
};
839+
const image = {
840+
...assistantMessage("2026-06-20T00:00:02.750Z"),
841+
id: TurnItemId.make("item-image"),
842+
messageId: MessageId.make("message-image"),
843+
text: hasImages ? "" : "Build is finishing.",
844+
attachments,
845+
};
826846
const feed = buildThreadFeed([
827847
projected(userMessage(), 0),
828848
projected(opening, 1),
829849
projected(command(), 2),
830850
projected(middle, 3),
831-
projected(assistantMessage(), 4),
851+
projected(image, 4),
852+
projected(assistantMessage(), 5),
832853
]);
833854
const latestRun = {
834855
runId,
@@ -841,6 +862,7 @@ describe("buildThreadFeed", () => {
841862
expect(collapsed.map((entry) => entry.id)).toEqual([
842863
"message-user",
843864
"run-fold:run-1",
865+
...(hasImages ? ["message-middle", "message-image"] : []),
844866
"message-assistant",
845867
]);
846868
expect(collapsed[1]).toMatchObject({
@@ -857,6 +879,7 @@ describe("buildThreadFeed", () => {
857879
"activity-group",
858880
"message",
859881
"message",
882+
"message",
860883
]);
861884
const anchor = {
862885
...command(),
@@ -884,8 +907,35 @@ describe("buildThreadFeed", () => {
884907
"message-user",
885908
"message-opening",
886909
"message-middle",
910+
"message-image",
887911
"message-assistant",
888912
]);
913+
914+
const steered = deriveThreadFeedPresentation(
915+
buildThreadFeed([
916+
projected(userMessage(), 0),
917+
projected(opening, 1),
918+
projected(
919+
{
920+
...userMessage("2026-06-20T00:00:02.000Z"),
921+
id: TurnItemId.make("item-steer"),
922+
messageId: MessageId.make("message-steer"),
923+
inputIntent: "steer",
924+
},
925+
2,
926+
),
927+
projected(middle, 3),
928+
projected(image, 4),
929+
projected(assistantMessage(), 5),
930+
]),
931+
latestRun,
932+
new Set(),
933+
);
934+
expect(
935+
steered.flatMap((entry) =>
936+
entry.type === "message" && entry.message.role === "assistant" ? [entry.id] : [],
937+
),
938+
).toEqual([...(hasImages ? ["message-middle", "message-image"] : []), "message-assistant"]);
889939
});
890940

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

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ function deriveThreadFeedRunFolds(
10841084
}
10851085
const groupsByRunId = new Map<
10861086
RunId,
1087-
{ entries: ThreadFeedEntry[]; startBoundary: string | null }
1087+
{ entries: ThreadFeedEntry[]; startBoundary: string | null; responseBoundaryIndex: number }
10881088
>();
10891089
let runlessKey: RunId | null = null;
10901090
let responseKey: RunId | null = null;
@@ -1094,6 +1094,8 @@ function deriveThreadFeedRunFolds(
10941094
pendingUserBoundary = entry.message.createdAt;
10951095
runlessKey = entry.message.runId == null ? RunId.make(`runless:${entry.id}`) : null;
10961096
responseKey = entry.message.runId ?? runlessKey;
1097+
const group = responseKey === null ? undefined : groupsByRunId.get(responseKey);
1098+
if (group) group.responseBoundaryIndex = group.entries.length;
10971099
continue;
10981100
}
10991101
const ownRunId =
@@ -1112,7 +1114,7 @@ function deriveThreadFeedRunFolds(
11121114
: ownRunId;
11131115
let group = groupsByRunId.get(runId);
11141116
if (!group) {
1115-
group = { entries: [], startBoundary: pendingUserBoundary };
1117+
group = { entries: [], startBoundary: pendingUserBoundary, responseBoundaryIndex: 0 };
11161118
pendingUserBoundary = null;
11171119
groupsByRunId.set(runId, group);
11181120
}
@@ -1148,11 +1150,26 @@ function deriveThreadFeedRunFolds(
11481150
continue;
11491151
}
11501152
const terminalAssistantId = terminalAssistantMessageIdByRun.get(runId);
1153+
const terminalEntryIndex = terminalAssistantId
1154+
? group.entries.findIndex((entry) => entry.id === terminalAssistantId)
1155+
: group.entries.length;
1156+
let responseStartIndex = terminalEntryIndex;
1157+
let responseHasImages = false;
1158+
// Inline images split one reply into consecutive text and attachment messages.
1159+
for (let index = terminalEntryIndex; index >= group.responseBoundaryIndex; index--) {
1160+
const entry = group.entries[index];
1161+
if (entry?.type !== "message" || entry.message.role !== "assistant") break;
1162+
responseStartIndex = index;
1163+
if (entry.message.attachments.some((attachment) => attachment.type === "image")) {
1164+
responseHasImages = true;
1165+
}
1166+
}
1167+
if (!responseHasImages) responseStartIndex = terminalEntryIndex;
11511168
const hiddenEntryIds = new Set(
11521169
group.entries
11531170
.filter(
1154-
(entry) =>
1155-
entry.id !== terminalAssistantId &&
1171+
(entry, index) =>
1172+
(index < responseStartIndex || index > terminalEntryIndex) &&
11561173
entry.type !== "html-render" &&
11571174
entry.type !== "mcp-app" &&
11581175
!(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) &&
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)