Skip to content

Commit 0a25a1a

Browse files
refacto(app/utils/chat.ts)r: optimize function preProcessImageContentBase
1 parent b709ee3 commit 0a25a1a

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

app/utils/chat.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ export function compressImage(file: Blob, maxSize: number): Promise<string> {
7070
});
7171
}
7272

73-
export async function preProcessImageContent(
73+
export async function preProcessImageContentBase(
7474
content: RequestMessage["content"],
75+
transformImageUrl: (url: string) => Promise<{ [key: string]: any }>,
7576
) {
7677
if (typeof content === "string") {
7778
return content;
@@ -81,7 +82,7 @@ export async function preProcessImageContent(
8182
if (part?.type == "image_url" && part?.image_url?.url) {
8283
try {
8384
const url = await cacheImageToBase64Image(part?.image_url?.url);
84-
result.push({ type: part.type, image_url: { url } });
85+
result.push(await transformImageUrl(url));
8586
} catch (error) {
8687
console.error("Error processing image URL:", error);
8788
}
@@ -92,26 +93,21 @@ export async function preProcessImageContent(
9293
return result;
9394
}
9495

96+
export async function preProcessImageContent(
97+
content: RequestMessage["content"],
98+
) {
99+
return preProcessImageContentBase(content, async (url) => ({
100+
type: "image_url",
101+
image_url: { url },
102+
}));
103+
}
104+
95105
export async function preProcessImageContentForAlibabaDashScope(
96106
content: RequestMessage["content"],
97107
) {
98-
if (typeof content === "string") {
99-
return content;
100-
}
101-
const result = [];
102-
for (const part of content) {
103-
if (part?.type == "image_url" && part?.image_url?.url) {
104-
try {
105-
const url = await cacheImageToBase64Image(part?.image_url?.url);
106-
result.push({ image: url });
107-
} catch (error) {
108-
console.error("Error processing image URL:", error);
109-
}
110-
} else {
111-
result.push({ ...part });
112-
}
113-
}
114-
return result;
108+
return preProcessImageContentBase(content, async (url) => ({
109+
image: url,
110+
}));
115111
}
116112

117113
const imageCaches: Record<string, string> = {};

0 commit comments

Comments
 (0)