diff --git a/src/sessions/user-turn-transcript.test.ts b/src/sessions/user-turn-transcript.test.ts index 60d8f6d1c70..dd2b4022356 100644 --- a/src/sessions/user-turn-transcript.test.ts +++ b/src/sessions/user-turn-transcript.test.ts @@ -51,5 +51,19 @@ describe("user turn transcript persistence", () => { MediaTypes: ["document", "application/octet-stream"], }); }); + + it("keeps media paths and types aligned when incomplete entries are skipped", () => { + expect( + buildPersistedUserTurnMediaFields([ + { contentType: "image/png" }, + { path: "/tmp/b.jpg", contentType: "image/jpeg" }, + ]), + ).toEqual({ + MediaPath: "/tmp/b.jpg", + MediaPaths: ["/tmp/b.jpg"], + MediaType: "image/jpeg", + MediaTypes: ["image/jpeg"], + }); + }); }); }); diff --git a/src/sessions/user-turn-transcript.ts b/src/sessions/user-turn-transcript.ts index c8a7e7eaddd..f53fbfa2bda 100644 --- a/src/sessions/user-turn-transcript.ts +++ b/src/sessions/user-turn-transcript.ts @@ -25,19 +25,34 @@ function mediaTypeForTranscript(media: PersistedUserTurnMediaInput): string { ); } +function normalizeMediaEntryForTranscript(media: PersistedUserTurnMediaInput): + | { + path: string; + type: string; + } + | undefined { + const path = normalizeOptionalText(media.path) ?? normalizeOptionalText(media.url); + if (!path) { + return undefined; + } + return { + path, + type: mediaTypeForTranscript(media), + }; +} + export function buildPersistedUserTurnMediaFields( media: readonly PersistedUserTurnMediaInput[] | null | undefined, ): PersistedUserTurnMediaFields { const entries = Array.isArray(media) ? media : []; - const paths = entries - .map((entry) => normalizeOptionalText(entry.path) ?? normalizeOptionalText(entry.url)) - .filter((path): path is string => Boolean(path)); + const normalized = entries + .map(normalizeMediaEntryForTranscript) + .filter((entry): entry is { path: string; type: string } => entry !== undefined); + const paths = normalized.map((entry) => entry.path); if (paths.length === 0) { return {}; } - const types = entries - .map((entry, index) => (paths[index] ? mediaTypeForTranscript(entry) : undefined)) - .filter((type): type is string => Boolean(type)); + const types = normalized.map((entry) => entry.type); return { MediaPath: paths[0], MediaPaths: paths,