Skip to content

Commit d1b9cc5

Browse files
refactor: 使用 reduce 重构 gemini-client 以修复类型错误
Co-authored-by: aider (vertex_ai/gemini-2.5-pro) <aider@aider.chat>
1 parent 5baad04 commit d1b9cc5

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

packages/mcp-server/src/gemini-client.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,22 @@ export class GeminiApiClient {
9898
}
9999

100100
if (Array.isArray(msg.content)) {
101-
const parts: Part[] = msg.content
102-
.map((part: MessageContentPart) => {
103-
if (part.type === 'text') {
104-
return { text: part.text || '' };
105-
}
106-
if (part.type === 'image_url' && part.image_url) {
107-
const imageUrl = part.image_url.url;
108-
if (imageUrl.startsWith('data:')) {
109-
const [mimePart, dataPart] = imageUrl.split(',');
110-
const mimeType = mimePart.split(':')[1].split(';')[0];
111-
return { inlineData: { mimeType, data: dataPart } };
112-
}
101+
const parts = msg.content.reduce<Part[]>((acc, part: MessageContentPart) => {
102+
if (part.type === 'text') {
103+
acc.push({ text: part.text || '' });
104+
} else if (part.type === 'image_url' && part.image_url) {
105+
const imageUrl = part.image_url.url;
106+
if (imageUrl.startsWith('data:')) {
107+
const [mimePart, dataPart] = imageUrl.split(',');
108+
const mimeType = mimePart.split(':')[1].split(';')[0];
109+
acc.push({ inlineData: { mimeType, data: dataPart } });
110+
} else {
113111
// Gemini API 更喜欢 inlineData,但 fileData 也可以作为备选
114-
return { fileData: { mimeType: 'image/jpeg', fileUri: imageUrl } };
112+
acc.push({ fileData: { mimeType: 'image/jpeg', fileUri: imageUrl } });
115113
}
116-
return null;
117-
})
118-
.filter((p): p is Part => p !== null);
114+
}
115+
return acc;
116+
}, []);
119117

120118
return { role, parts };
121119
}

0 commit comments

Comments
 (0)