Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions apps/mobile/src/lib/threadActivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ChatAttachmentId,
MessageId,
CheckpointId,
CheckpointRef,
Expand Down Expand Up @@ -810,25 +811,45 @@ describe("buildThreadFeed", () => {
);
});

it("folds progress messages while keeping the final assistant answer visible", () => {
it.each([false, true])("folds progress before a reply with images: %s", (hasImages) => {
const attachments = hasImages
? [
{
type: "image" as const,
id: ChatAttachmentId.make("reply-image"),
name: "Reply",
mimeType: "image/png",
sizeBytes: 401,
},
]
: [];
const opening = {
...assistantMessage("2026-06-20T00:00:01.500Z"),
id: TurnItemId.make("item-opening"),
messageId: MessageId.make("message-opening"),
text: "I will check the deployment configuration.",
attachments,
};
const middle = {
...assistantMessage("2026-06-20T00:00:02.500Z"),
id: TurnItemId.make("item-middle"),
messageId: MessageId.make("message-middle"),
text: "The configuration is valid; checking the build next.",
};
const image = {
...assistantMessage("2026-06-20T00:00:02.750Z"),
id: TurnItemId.make("item-image"),
messageId: MessageId.make("message-image"),
text: hasImages ? "" : "Build is finishing.",
attachments,
};
const feed = buildThreadFeed([
projected(userMessage(), 0),
projected(opening, 1),
projected(command(), 2),
projected(middle, 3),
projected(assistantMessage(), 4),
projected(image, 4),
projected(assistantMessage(), 5),
]);
const latestRun = {
runId,
Expand All @@ -841,6 +862,7 @@ describe("buildThreadFeed", () => {
expect(collapsed.map((entry) => entry.id)).toEqual([
"message-user",
"run-fold:run-1",
...(hasImages ? ["message-middle", "message-image"] : []),
"message-assistant",
]);
expect(collapsed[1]).toMatchObject({
Expand All @@ -857,6 +879,7 @@ describe("buildThreadFeed", () => {
"activity-group",
"message",
"message",
"message",
]);
const anchor = {
...command(),
Expand Down Expand Up @@ -884,8 +907,35 @@ describe("buildThreadFeed", () => {
"message-user",
"message-opening",
"message-middle",
"message-image",
"message-assistant",
]);

const steered = deriveThreadFeedPresentation(
buildThreadFeed([
projected(userMessage(), 0),
projected(opening, 1),
projected(
{
...userMessage("2026-06-20T00:00:02.000Z"),
id: TurnItemId.make("item-steer"),
messageId: MessageId.make("message-steer"),
inputIntent: "steer",
},
2,
),
projected(middle, 3),
projected(image, 4),
projected(assistantMessage(), 5),
]),
latestRun,
new Set(),
);
expect(
steered.flatMap((entry) =>
entry.type === "message" && entry.message.role === "assistant" ? [entry.id] : [],
),
).toEqual([...(hasImages ? ["message-middle", "message-image"] : []), "message-assistant"]);
});

// regression: #21393 show plans and fold settled progress notes
Expand Down
25 changes: 21 additions & 4 deletions apps/mobile/src/lib/threadActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ function deriveThreadFeedRunFolds(
}
const groupsByRunId = new Map<
RunId,
{ entries: ThreadFeedEntry[]; startBoundary: string | null }
{ entries: ThreadFeedEntry[]; startBoundary: string | null; responseBoundaryIndex: number }
>();
let runlessKey: RunId | null = null;
let responseKey: RunId | null = null;
Expand All @@ -1094,6 +1094,8 @@ function deriveThreadFeedRunFolds(
pendingUserBoundary = entry.message.createdAt;
runlessKey = entry.message.runId == null ? RunId.make(`runless:${entry.id}`) : null;
responseKey = entry.message.runId ?? runlessKey;
const group = responseKey === null ? undefined : groupsByRunId.get(responseKey);
if (group) group.responseBoundaryIndex = group.entries.length;
continue;
}
const ownRunId =
Expand All @@ -1112,7 +1114,7 @@ function deriveThreadFeedRunFolds(
: ownRunId;
let group = groupsByRunId.get(runId);
if (!group) {
group = { entries: [], startBoundary: pendingUserBoundary };
group = { entries: [], startBoundary: pendingUserBoundary, responseBoundaryIndex: 0 };
pendingUserBoundary = null;
groupsByRunId.set(runId, group);
}
Expand Down Expand Up @@ -1148,11 +1150,26 @@ function deriveThreadFeedRunFolds(
continue;
}
const terminalAssistantId = terminalAssistantMessageIdByRun.get(runId);
const terminalEntryIndex = terminalAssistantId
? group.entries.findIndex((entry) => entry.id === terminalAssistantId)
: group.entries.length;
let responseStartIndex = terminalEntryIndex;
let responseHasImages = false;
// Inline images split one reply into consecutive text and attachment messages.
for (let index = terminalEntryIndex; index >= group.responseBoundaryIndex; index--) {
const entry = group.entries[index];
if (entry?.type !== "message" || entry.message.role !== "assistant") break;
responseStartIndex = index;
if (entry.message.attachments.some((attachment) => attachment.type === "image")) {
responseHasImages = true;
}
}
if (!responseHasImages) responseStartIndex = terminalEntryIndex;
const hiddenEntryIds = new Set(
group.entries
.filter(
(entry) =>
entry.id !== terminalAssistantId &&
(entry, index) =>
(index < responseStartIndex || index > terminalEntryIndex) &&
entry.type !== "html-render" &&
entry.type !== "mcp-app" &&
!(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) &&
Expand Down
79 changes: 79 additions & 0 deletions apps/server/src/assets/ChatAttachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
ChatAttachmentId,
PersistChatAttachmentsError,
type PersistChatAttachmentsInput,
} from "@t3tools/contracts";
import * as Base64 from "effect/encoding/Base64";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

import {
attachmentRelativePath,
chatAttachmentStableKey,
createDeterministicAttachmentId,
} from "../attachmentStore.ts";
import * as ServerConfig from "../config.ts";
import { parseBase64DataUrl } from "../imageMime.ts";

export const persistChatAttachments = Effect.fn("ws.assets.persistChatAttachments")(function* (
input: PersistChatAttachmentsInput,
) {
const config = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return yield* Effect.forEach(
input.attachments.map((attachment, index) => ({ attachment, index })),
Effect.fn("ws.assets.persistChatAttachment")(function* ({ attachment, index }) {
const parsed = parseBase64DataUrl(attachment.dataUrl);
if (parsed === null || parsed.mimeType !== attachment.mimeType.toLowerCase()) {
return yield* new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} has an invalid image payload.`,
});
}
const bytes = yield* Effect.fromResult(Base64.decode(parsed.base64)).pipe(
Effect.mapError(
(cause) =>
new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} is not valid base64.`,
cause,
}),
),
);
if (bytes.byteLength !== attachment.sizeBytes) {
return yield* new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} size does not match its payload.`,
});
}
const rawId = createDeterministicAttachmentId(
input.threadId,
chatAttachmentStableKey(input.messageId, index, bytes),
);
if (rawId === null) {
return yield* new PersistChatAttachmentsError({
message: "Could not allocate an attachment identifier.",
});
}
const persisted = {
type: "image" as const,
id: ChatAttachmentId.make(rawId),
name: attachment.name,
mimeType: attachment.mimeType,
sizeBytes: attachment.sizeBytes,
};
yield* fileSystem
.writeFile(path.join(config.attachmentsDir, attachmentRelativePath(persisted)!), bytes)
.pipe(
Effect.mapError(
(cause) =>
new PersistChatAttachmentsError({
message: `Could not persist attachment ${attachment.name}.`,
cause,
}),
),
);
return persisted;
}),
{ concurrency: 2 },
);
});
Loading
Loading