mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 11:42:55 +00:00
fix: keep user turn media fields aligned
This commit is contained in:
@@ -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"],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user