fix(gateway): reconcile attachment offload rebase

This commit is contained in:
samzong
2026-04-28 01:28:44 +08:00
committed by Frank Yang
parent b63b33adb6
commit 3dae8653eb
4 changed files with 20 additions and 10 deletions

View File

@@ -8,7 +8,9 @@ const saveMediaBufferMock = vi.hoisted(() =>
contentType: mime,
})),
);
const deleteMediaBufferMock = vi.hoisted(() => vi.fn(async () => undefined));
const deleteMediaBufferMock = vi.hoisted(() =>
vi.fn(async (_id: string, _subdir?: string) => undefined),
);
vi.mock("../media/store.js", async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>;

View File

@@ -271,7 +271,12 @@ export async function parseMessageWithAttachments(
labelMime ||
"application/octet-stream";
if (sniffedMime && providedMime && sniffedMime !== providedMime) {
if (
sniffedMime &&
providedMime &&
!isGenericContainerMime(providedMime) &&
sniffedMime !== providedMime
) {
const usedSource =
finalMime === sniffedMime
? "sniffed"
@@ -309,7 +314,11 @@ export async function parseMessageWithAttachments(
);
}
if (shouldForceImageOffload && isImage && textOnlyImageOffloadCount >= TEXT_ONLY_OFFLOAD_LIMIT) {
if (
shouldForceImageOffload &&
isImage &&
textOnlyImageOffloadCount >= TEXT_ONLY_OFFLOAD_LIMIT
) {
log?.warn(
`attachment ${label}: dropping image because text-only offload limit ` +
`${TEXT_ONLY_OFFLOAD_LIMIT} was reached`,
@@ -318,7 +327,8 @@ export async function parseMessageWithAttachments(
continue;
}
const shouldOffload = shouldForceImageOffload || !isImage || sizeBytes > OFFLOAD_THRESHOLD_BYTES;
const shouldOffload =
shouldForceImageOffload || !isImage || sizeBytes > OFFLOAD_THRESHOLD_BYTES;
if (!shouldOffload) {
images.push({ type: "image", data: b64, mimeType: finalMime });

View File

@@ -2022,10 +2022,8 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
}
| undefined;
expect(mockState.lastDispatchImages).toBeUndefined();
expect(mockState.lastDispatchImageOrder).toEqual(["offloaded"]);
expect(mockState.lastDispatchCtx?.Body).toMatch(
/^summarize this\n\[media attached: media:\/\/inbound\//,
);
expect(mockState.lastDispatchImageOrder).toBeUndefined();
expect(mockState.lastDispatchCtx?.Body).toBe("summarize this");
expect(mockState.savedMediaCalls).toEqual([
expect.objectContaining({
contentType: "application/pdf",
@@ -2033,7 +2031,7 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
size: expect.any(Number),
}),
]);
expect(message?.content).toMatch(/^summarize this\n\[media attached: media:\/\/inbound\//);
expect(message?.content).toBe("summarize this");
expect(message?.MediaPath).toBe("/tmp/chat-send-brief.pdf");
expect(message?.MediaPaths).toEqual(["/tmp/chat-send-brief.pdf"]);
expect(message?.MediaType).toBe("application/pdf");

View File

@@ -69,7 +69,7 @@ import {
projectRecentChatDisplayMessages,
resolveEffectiveChatHistoryMaxChars,
} from "../chat-display-projection.js";
import { stripEnvelopeFromMessage, stripEnvelopeFromMessages } from "../chat-sanitize.js";
import { stripEnvelopeFromMessage } from "../chat-sanitize.js";
import { augmentChatHistoryWithCliSessionImports } from "../cli-session-history.js";
import { isSuppressedControlReplyText } from "../control-reply-text.js";
import {