fix: keep user turn media fields aligned

This commit is contained in:
Shakker
2026-05-25 13:52:04 +01:00
committed by Shakker
parent f65fec27a2
commit 4a4ef7be5e
2 changed files with 35 additions and 6 deletions

View File

@@ -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"],
});
});
});
});

View File

@@ -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,